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.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 β€ n β€ 2β
10^5) β the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
n = int(input())
p = [0]
for i in input().split():
p.append(p[-1] + int(i))
# print(p)
p = [p[i] + (n - max(p)) for i in range(n)]
# print(p)
s = [i for i in range(1, n + 1)]
if(sorted(p) != s):
print(-1)
else:
print(p)
``` | instruction | 0 | 43,876 | 12 | 87,752 |
No | output | 1 | 43,876 | 12 | 87,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 β€ n β€ 2β
10^5) β the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
import time
start_time = time.time()
import collections
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
"""
We know that P0 is Q0 less than P1, (Q0+Q1) less than P2, etc...
Set first element as 1, then calculate the rest of the array from there.
If len(set) < N, or max(set)-min(set) != N-1, -1
else add on 1-(min(set)) to every element
"""
def solve():
N = getInt()
Q = getInts()
arr = [0]
for q in Q:
arr.append(arr[-1]+q)
max_arr = max(arr)
min_arr = min(arr)
if len(set(arr)) != N or max_arr-min_arr != N-1:
return -1
arr = [arr[j]+1-min_arr for j in range(N)]
print(*arr)
return
solve()
``` | instruction | 0 | 43,877 | 12 | 87,754 |
No | output | 1 | 43,877 | 12 | 87,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 β€ n β€ 2β
10^5) β the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
from sys import stdin as sin
n = int(input())
s = [ int(i) for i in sin.readline().split(' ')]
tsum = sum(s)
#goal = n * (n + 1)
#goal /= 2
#ans_set = set([i + 1 for i in range(n)])
for i in range(1, n + 1):
if tsum + i > n or tsum + i < 0:
continue
ans = str(i)
#tans_set = set([i])
p = i
success = True
#tsum = i
for j in s:
ns = p + j
#if (ns < 1 or ns > n) or ns in tans_set:
if (ns < 1 or ns > n):
success = False
break
ans += ' ' + str(ns)
#tans_set.add(ns)
p = ns
#tsum += ns
#if tsum > goal:
# success = False
# break
if not success:
continue
#if sum(ans) == goal and sorted(ans) == sorted_ans:
#if tsum == goal and set(ans) == ans_set:
#if tsum == goal:
print(ans)
exit()
print(-1)
``` | instruction | 0 | 43,878 | 12 | 87,756 |
No | output | 1 | 43,878 | 12 | 87,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define a balanced multiset the following way. Write down the sum of all elements of the multiset in its decimal representation. For each position of that number check if the multiset includes at least one element such that the digit of the element and the digit of the sum at that position are the same. If that holds for every position, then the multiset is balanced. Otherwise it's unbalanced.
For example, multiset \{20, 300, 10001\} is balanced and multiset \{20, 310, 10001\} is unbalanced:
<image>
The red digits mark the elements and the positions for which these elements have the same digit as the sum. The sum of the first multiset is 10321, every position has the digit required. The sum of the second multiset is 10331 and the second-to-last digit doesn't appear in any number, thus making the multiset unbalanced.
You are given an array a_1, a_2, ..., a_n, consisting of n integers.
You are asked to perform some queries on it. The queries can be of two types:
* 1~i~x β replace a_i with the value x;
* 2~l~r β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the minimum sum, or report that no unbalanced subset exists.
Note that the empty multiset is balanced.
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Input
The first line contains two integers n and m (1 β€ n, m β€ 2 β
10^5) β the number of elements in the array and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i < 10^9).
Each of the following m lines contains a query of one of two types:
* 1~i~x (1 β€ i β€ n, 1 β€ x < 10^9) β replace a_i with the value x;
* 2~l~r (1 β€ l β€ r β€ n) β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the lowest sum, or report that no unbalanced subset exists.
It is guaranteed that there is at least one query of the second type.
Output
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Example
Input
4 5
300 10001 20 20
2 1 3
1 1 310
2 1 3
2 3 3
2 3 4
Output
-1
330
-1
40
Note
All the subsets of multiset \{20, 300, 10001\} are balanced, thus the answer is -1.
The possible unbalanced subsets in the third query are \{20, 310\} and \{20, 310, 10001\}. The lowest sum one is \{20, 310\}. Note that you are asked to choose a subset, not a subsegment, thus the chosen elements might not be adjancent in the array.
The fourth query includes only the empty subset and subset \{20\}. Both of them are balanced.
The last query includes the empty subset and the subsets \{20\}, \{20\} and \{20, 20\}. Only \{20, 20\} is unbalanced, its sum is 40. Note that you are asked to choose a multiset, thus it might include equal elements.
Submitted Solution:
```
import sys
input = sys.stdin.readline
INT_MAX = 10**18
class SegTree:
def __init__(self, idx, l, r):
self.idx = idx
self.l = l
self.r = r
self.mid = (l+r) // 2
self.record = [0] * 10
self.value = 0
if self.l != self.r:
mid = (l + r) // 2
self.left = SegTree(idx*2, l, mid)
self.right = SegTree(idx*2 + 1, mid + 1, r)
def query(self, l, r):
if self.l == l and self.r == r:
return self.value, self.record
if r <= self.mid:
return self.left.query(l, r)
elif self.mid < l:
return self.right.query(l, r)
else:
lv, lr = self.left.query(l, self.mid)
rv, rr = self.right.query(self.mid+1, r)
tmp_ans = INT_MAX
if lv:
tmp_ans = min(lv, tmp_ans)
if rv:
tmp_ans = min(rv, tmp_ans)
nr = []
for i, j in zip(lr, rr):
if i and j:
tmp_ans = min(i+j, tmp_ans)
elif i:
nr.append(i)
elif j:
nr.append(j)
else:
nr.append(0)
return 0 if tmp_ans == INT_MAX else tmp_ans, nr
def edit(self, idx, value):
if self.l == idx and self.r == idx:
t_value = value
for i in range(10):
if t_value % 10:
self.record[i] = value
else:
self.record[i] = 0
t_value //= 10
self.value = 0
return
if idx <= self.mid:
self.left.edit(idx, value)
else:
self.right.edit(idx, value)
lv, lr = self.left.query(self.l, self.mid)
rv, rr = self.right.query(self.mid+1, self.r)
tmp_ans = INT_MAX
if lv:
tmp_ans = min(lv, tmp_ans)
if rv:
tmp_ans = min(rv, tmp_ans)
for idx, (i, j) in enumerate(zip(lr, rr)):
if i and j:
tmp_ans = min(i+j, tmp_ans)
self.record[idx] = INT_MAX
if i:
self.record[idx] = min(self.record[idx], i)
if j:
self.record[idx] = min(self.record[idx], j)
if self.record[idx] == INT_MAX:
self.record[idx] = 0
self.value = 0 if tmp_ans == INT_MAX else tmp_ans
if __name__ == '__main__':
n, q = map(int, input().split())
nums = list(map(int, input().split()))
seg_tree = SegTree(1, 1, n)
for idx, num in enumerate(nums):
seg_tree.edit(idx+1, num)
for _ in range(q):
t, l, r = map(int, input().split())
print(t, l, r)
if t == 1:
seg_tree.edit(l, r)
else:
ans, _ = seg_tree.query(l, r)
print(ans if ans else -1)
``` | instruction | 0 | 43,927 | 12 | 87,854 |
No | output | 1 | 43,927 | 12 | 87,855 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define a balanced multiset the following way. Write down the sum of all elements of the multiset in its decimal representation. For each position of that number check if the multiset includes at least one element such that the digit of the element and the digit of the sum at that position are the same. If that holds for every position, then the multiset is balanced. Otherwise it's unbalanced.
For example, multiset \{20, 300, 10001\} is balanced and multiset \{20, 310, 10001\} is unbalanced:
<image>
The red digits mark the elements and the positions for which these elements have the same digit as the sum. The sum of the first multiset is 10321, every position has the digit required. The sum of the second multiset is 10331 and the second-to-last digit doesn't appear in any number, thus making the multiset unbalanced.
You are given an array a_1, a_2, ..., a_n, consisting of n integers.
You are asked to perform some queries on it. The queries can be of two types:
* 1~i~x β replace a_i with the value x;
* 2~l~r β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the minimum sum, or report that no unbalanced subset exists.
Note that the empty multiset is balanced.
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Input
The first line contains two integers n and m (1 β€ n, m β€ 2 β
10^5) β the number of elements in the array and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i < 10^9).
Each of the following m lines contains a query of one of two types:
* 1~i~x (1 β€ i β€ n, 1 β€ x < 10^9) β replace a_i with the value x;
* 2~l~r (1 β€ l β€ r β€ n) β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the lowest sum, or report that no unbalanced subset exists.
It is guaranteed that there is at least one query of the second type.
Output
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Example
Input
4 5
300 10001 20 20
2 1 3
1 1 310
2 1 3
2 3 3
2 3 4
Output
-1
330
-1
40
Note
All the subsets of multiset \{20, 300, 10001\} are balanced, thus the answer is -1.
The possible unbalanced subsets in the third query are \{20, 310\} and \{20, 310, 10001\}. The lowest sum one is \{20, 310\}. Note that you are asked to choose a subset, not a subsegment, thus the chosen elements might not be adjancent in the array.
The fourth query includes only the empty subset and subset \{20\}. Both of them are balanced.
The last query includes the empty subset and the subsets \{20\}, \{20\} and \{20, 20\}. Only \{20, 20\} is unbalanced, its sum is 40. Note that you are asked to choose a multiset, thus it might include equal elements.
Submitted Solution:
```
maxNum = 0
def isF(fuckSet,comp):
global maxNum
ans = 0xffffff
for each in fuckSet:
for i in range(maxNum):
if i < len(each) and int(each[i])>0 :
if comp[i][0] == 0 or comp[i][0] > int(each[::-1]):
comp[i][1] = comp[i][0]
comp[i][0] = int(each[::-1])
elif comp[i][1] == 0 or comp[i][1] > int(each[::-1]):
comp[i][1] = int(each[::-1])
if comp[i][0] > 0 and comp[i][1] > 0:
if ans > comp[i][0] + comp[i][1]:
ans = comp[i][0] + comp[i][1]
if ans == 0xffffff:
return -1
return ans
def reC(comp):
ans = 0xffffff
for each in comp:
if each[0] > 0 and each[1] > 0 and each[0] + each[1] < ans:
ans = each[0] + each[1]
return ans
def forDis(fuckSet,src,dst,comp):
# print(fuckSet[dst[0]:src[0]])
# print(fuckSet[src[1]+1:dst[1]+1])
minAns = isF(fuckSet[dst[0]:src[0]],comp)
minAns2 = isF(fuckSet[src[1]+1:dst[1]+1],comp)
if minAns2 == -1:
minAns2 = reC(comp)
return minAns2
def contMax(fuck):
global maxNum
if len(fuck) > maxNum:
maxNum = len(fuck)
return fuck[::-1]
def minDis(D,l,r):
if len(D) <= 0:
return -1
minD = 0xffffff
res = []
resi = 0
index = 0
for i in D:
if i[0] >= l and i[1] <= r and minD > (i[0] - l) + (i[1] - r):
minD = (i[0] - l) + (r - i[1])
res = i
resi = index
index += 1
del D[resi]
if minD == 0xffffff:
return -1
return i
fuckSet = []
(fuckNum,opNum) = map(int,input().split())
fuckSet = list(map(contMax,input().split()))
D = []
for i in range(opNum):
opt = list(map(int,input().split()))
if opt[0] == 2:
l = opt[1]
r = opt[2]
shot = minDis(D,l,r)
if shot == -1:
cp = [[0, 0] for i in range(maxNum)]
ans = isF(fuckSet[opt[1] - 1:opt[2]], cp)
D.append([l,r,cp])
else:
# print("aya",end = '')
ans = forDis(fuckSet, shot, [l-1, r], shot[2])
D.append([l, r, shot[2]])
print(ans)
if D[-1][2] == -1:
del D[-1]
elif opt[0] == 1:
fuckSet[int(opt[1]) - 1] = str(opt[2])[::-1]
# print(fuckSet)
if maxNum < len(str(opt[2])):
maxNum = len(str(opt[2]))
for i in range(len(D)):
if D[i][0] <= opt[1] and D[i][1] >= opt[2]:
isF([fuckSet[int(opt[1]) - 1]], D[i][2])
``` | instruction | 0 | 43,928 | 12 | 87,856 |
No | output | 1 | 43,928 | 12 | 87,857 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define a balanced multiset the following way. Write down the sum of all elements of the multiset in its decimal representation. For each position of that number check if the multiset includes at least one element such that the digit of the element and the digit of the sum at that position are the same. If that holds for every position, then the multiset is balanced. Otherwise it's unbalanced.
For example, multiset \{20, 300, 10001\} is balanced and multiset \{20, 310, 10001\} is unbalanced:
<image>
The red digits mark the elements and the positions for which these elements have the same digit as the sum. The sum of the first multiset is 10321, every position has the digit required. The sum of the second multiset is 10331 and the second-to-last digit doesn't appear in any number, thus making the multiset unbalanced.
You are given an array a_1, a_2, ..., a_n, consisting of n integers.
You are asked to perform some queries on it. The queries can be of two types:
* 1~i~x β replace a_i with the value x;
* 2~l~r β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the minimum sum, or report that no unbalanced subset exists.
Note that the empty multiset is balanced.
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Input
The first line contains two integers n and m (1 β€ n, m β€ 2 β
10^5) β the number of elements in the array and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i < 10^9).
Each of the following m lines contains a query of one of two types:
* 1~i~x (1 β€ i β€ n, 1 β€ x < 10^9) β replace a_i with the value x;
* 2~l~r (1 β€ l β€ r β€ n) β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the lowest sum, or report that no unbalanced subset exists.
It is guaranteed that there is at least one query of the second type.
Output
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Example
Input
4 5
300 10001 20 20
2 1 3
1 1 310
2 1 3
2 3 3
2 3 4
Output
-1
330
-1
40
Note
All the subsets of multiset \{20, 300, 10001\} are balanced, thus the answer is -1.
The possible unbalanced subsets in the third query are \{20, 310\} and \{20, 310, 10001\}. The lowest sum one is \{20, 310\}. Note that you are asked to choose a subset, not a subsegment, thus the chosen elements might not be adjancent in the array.
The fourth query includes only the empty subset and subset \{20\}. Both of them are balanced.
The last query includes the empty subset and the subsets \{20\}, \{20\} and \{20, 20\}. Only \{20, 20\} is unbalanced, its sum is 40. Note that you are asked to choose a multiset, thus it might include equal elements.
Submitted Solution:
```
maxNum = 0
def isF(fuckSet):
global maxNum
ans = []
comp = [[0,0] for i in range(maxNum)]
for i in range(maxNum):
for each in fuckSet:
if i < len(each) and int(each[i])>0 :
if comp[i][0] == 0 or comp[i][0] > int(each[::-1]):
comp[i][1] = comp[i][0]
comp[i][0] = int(each[::-1])
elif comp[i][1] == 0 or comp[i][1] > int(each[::-1]):
comp[i][1] = int(each[::-1])
if comp[i][0] > 0 and comp[i][1] > 0:
return comp[i][0] + comp[i][1]
return -1
def contMax(fuck):
global maxNum
if len(fuck) > maxNum:
maxNum = len(fuck)
return fuck[::-1]
fuckSet = []
(fuckNum,opNum) = map(int,input().split())
fuckSet = list(map(contMax,input().split()))
for i in range(opNum):
opt = list(map(int,input().split()))
if opt[0] == 2:
print(isF(fuckSet[opt[1]-1:opt[2]]))
elif opt[0] == 1:
fuckSet[int(opt[1]) - 1] = str(opt[2])[::-1]
if maxNum < len(str(opt[2])):
maxNum = len(str(opt[2]))
``` | instruction | 0 | 43,929 | 12 | 87,858 |
No | output | 1 | 43,929 | 12 | 87,859 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define a balanced multiset the following way. Write down the sum of all elements of the multiset in its decimal representation. For each position of that number check if the multiset includes at least one element such that the digit of the element and the digit of the sum at that position are the same. If that holds for every position, then the multiset is balanced. Otherwise it's unbalanced.
For example, multiset \{20, 300, 10001\} is balanced and multiset \{20, 310, 10001\} is unbalanced:
<image>
The red digits mark the elements and the positions for which these elements have the same digit as the sum. The sum of the first multiset is 10321, every position has the digit required. The sum of the second multiset is 10331 and the second-to-last digit doesn't appear in any number, thus making the multiset unbalanced.
You are given an array a_1, a_2, ..., a_n, consisting of n integers.
You are asked to perform some queries on it. The queries can be of two types:
* 1~i~x β replace a_i with the value x;
* 2~l~r β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the minimum sum, or report that no unbalanced subset exists.
Note that the empty multiset is balanced.
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Input
The first line contains two integers n and m (1 β€ n, m β€ 2 β
10^5) β the number of elements in the array and the number of queries, respectively.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i < 10^9).
Each of the following m lines contains a query of one of two types:
* 1~i~x (1 β€ i β€ n, 1 β€ x < 10^9) β replace a_i with the value x;
* 2~l~r (1 β€ l β€ r β€ n) β find the unbalanced subset of the multiset of the numbers a_l, a_{l + 1}, ..., a_r with the lowest sum, or report that no unbalanced subset exists.
It is guaranteed that there is at least one query of the second type.
Output
For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists.
Example
Input
4 5
300 10001 20 20
2 1 3
1 1 310
2 1 3
2 3 3
2 3 4
Output
-1
330
-1
40
Note
All the subsets of multiset \{20, 300, 10001\} are balanced, thus the answer is -1.
The possible unbalanced subsets in the third query are \{20, 310\} and \{20, 310, 10001\}. The lowest sum one is \{20, 310\}. Note that you are asked to choose a subset, not a subsegment, thus the chosen elements might not be adjancent in the array.
The fourth query includes only the empty subset and subset \{20\}. Both of them are balanced.
The last query includes the empty subset and the subsets \{20\}, \{20\} and \{20, 20\}. Only \{20, 20\} is unbalanced, its sum is 40. Note that you are asked to choose a multiset, thus it might include equal elements.
Submitted Solution:
```
n,m = map(int, input().split())
a=[]
output=[]
def process_input(a_input):
b = a_input
for j in range(0,9):
if(b%10==0):
b=b/10
else:
return([a_input,j+1])
# for ex: if a_input = 30, then a <-(append) [3,1]
# a_input = 4000,then a <-(append) [4000,4]
'''
for i in range(n):
a_input = int (input())
a.append(process_input(a_input))
'''
for i in map(int, input().split()):
a.append(process_input(i))
#print(a)
for j in range(m):
q,l,r = map(int, input().split())
if(q==1):
i = l-1
x = r
a[i] = process_input(x)
if(q==2):
l-=1
a_cut = a[l:r]
o=10000000000
for k in range(1,9):
a_cut_k = [value for value in a_cut if value[1]==k]
#print(a_cut_k)
if len(a_cut_k)>=2:
a_cut_k = sorted(a_cut_k, key=lambda value: value[0])
#print(sorted(a_cut_k, key=lambda value: value[0]))
o = min (o, a_cut_k[0][0]+a_cut_k[1][0])
if o == 10000000000:
output.append(-1)
else:
output.append(o)
for i in range(len(output)):
print(output[i])
``` | instruction | 0 | 43,930 | 12 | 87,860 |
No | output | 1 | 43,930 | 12 | 87,861 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,931 | 12 | 87,862 |
Tags: dp, greedy, two pointers
Correct Solution:
```
import os
import sys
def solve(arr):
items = sorted(set(arr))
min_max = [(float("inf"), float("-inf"))] * len(items)
item_to_idx = {k: idx for idx, k in enumerate(items)}
for idx, a in enumerate(arr):
m, M = min_max[item_to_idx[a]]
min_max[item_to_idx[a]] = (min(idx, m), max(idx, M))
best = 1
current = 1
for i in range(1, len(items)):
_, prev_M = min_max[i - 1]
m, _ = min_max[i]
if prev_M <= m:
current += 1
else:
current = 1
best = max(best, current)
return len(items) - best
def pp(input):
T = int(input())
for t in range(T):
input()
arr = list(map(int, input().strip().split()))
print(solve(arr))
if "paalto" in os.getcwd():
from string_source import string_source, codeforces_parse
pp(
string_source(
"""3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7"""
)
)
else:
pp(sys.stdin.readline)
``` | output | 1 | 43,931 | 12 | 87,863 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,932 | 12 | 87,864 |
Tags: dp, greedy, two pointers
Correct Solution:
```
import sys as _sys
def main():
q = int(input())
for i_q in range(q):
n, = _read_ints()
a = tuple(_read_ints())
result = find_min_sorting_cost(sequence=a)
print(result)
def _read_line():
result = _sys.stdin.readline()
assert result[-1] == "\n"
return result[:-1]
def _read_ints():
return map(int, _read_line().split(" "))
def find_min_sorting_cost(sequence):
sequence = tuple(sequence)
if not sequence:
return 0
indices_by_values = {x: [] for x in sequence}
for i, x in enumerate(sequence):
indices_by_values[x].append(i)
borders_by_values = {
x: (indices[0], indices[-1]) for x, indices in indices_by_values.items()
}
borders_sorted_by_values = [borders for x, borders in sorted(borders_by_values.items())]
max_cost_can_keep_n = curr_can_keep_n = 1
for prev_border, curr_border in zip(borders_sorted_by_values, borders_sorted_by_values[1:]):
if curr_border[0] > prev_border[1]:
curr_can_keep_n += 1
else:
if curr_can_keep_n > max_cost_can_keep_n:
max_cost_can_keep_n = curr_can_keep_n
curr_can_keep_n = 1
if curr_can_keep_n > max_cost_can_keep_n:
max_cost_can_keep_n = curr_can_keep_n
return len(set(sequence)) - max_cost_can_keep_n
if __name__ == '__main__':
main()
``` | output | 1 | 43,932 | 12 | 87,865 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,933 | 12 | 87,866 |
Tags: dp, greedy, two pointers
Correct Solution:
```
# |
# _` | __ \ _` | __| _ \ __ \ _` | _` |
# ( | | | ( | ( ( | | | ( | ( |
# \__,_| _| _| \__,_| \___| \___/ _| _| \__,_| \__,_|
import sys
import math
def read_line():
return sys.stdin.readline()[:-1]
def read_int():
return int(sys.stdin.readline())
def read_int_line():
return [int(v) for v in sys.stdin.readline().split()]
def read_float_line():
return [float(v) for v in sys.stdin.readline().split()]
t = read_int()
for i in range(t):
n = read_int()
a = read_int_line()
d = {}
for i in range(n):
if a[i] in d:
d[a[i]].append(i)
else:
d[a[i]] = [i]
dp = [1]*len(list(d.keys()))
s = list(d.keys())
s.sort()
for i in range(len(s)-2,-1,-1):
if d[s[i]][-1] < d[s[i+1]][0]:
dp[i] = dp[i+1]+1
else:
dp[i] = 1
ans = len(s)-max(dp)
print(ans)
``` | output | 1 | 43,933 | 12 | 87,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,934 | 12 | 87,868 |
Tags: dp, greedy, two pointers
Correct Solution:
```
from sys import stdin
input = stdin.readline
def main():
anses = []
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
f = [0]*(n+1)
d = sorted(list(set(a)))
for q in range(1, len(d)+1):
f[d[q-1]] = q
for q in range(len(a)):
a[q] = f[a[q]]
n = len(d)
starts, ends = [-1]*(n+1), [n+1]*(n+1)
for q in range(len(a)):
if starts[a[q]] == -1:
starts[a[q]] = q
ends[a[q]] = q
s = [0]*(n+1)
max1 = -float('inf')
for q in range(1, n+1):
s[q] = s[q-1]*(ends[q-1] < starts[q])+1
max1 = max(max1, s[q])
anses.append(str(len(d)-max1))
print('\n'.join(anses))
main()
``` | output | 1 | 43,934 | 12 | 87,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,935 | 12 | 87,870 |
Tags: dp, greedy, two pointers
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n=int(input())
A=list(map(int,input().split()))
compression_dict={a:id for id,a in enumerate(sorted(set(A)))}
A=[compression_dict[a] for a in A]
lenth=len(A)
l=[-1]*(lenth)
r=[-1]*(lenth)
tot=0
for i in range(lenth):
if l[A[i]]==-1:
tot+=1
l[A[i]]=i
r[A[i]]=i
else:
r[A[i]]=i
ans=0
sum=1
for i in range(1,tot):
if l[i]>r[i-1]:
sum+=1
else:
ans=max(ans,sum)
sum=1
ans=max(ans,sum)
print(tot-ans)
``` | output | 1 | 43,935 | 12 | 87,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,936 | 12 | 87,872 |
Tags: dp, greedy, two pointers
Correct Solution:
```
def main():
from sys import stdin, stdout
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
inp1 = [-1] * (n + 1)
inp2 = [-1] * (n + 1)
for i, ai in enumerate(map(int, stdin.readline().split())):
if inp1[ai] < 0:
inp1[ai] = i
inp2[ai] = i
inp1 = tuple((inp1i for inp1i in inp1 if inp1i >= 0))
inp2 = tuple((inp2i for inp2i in inp2 if inp2i >= 0))
n = len(inp1)
ans = 0
cur = 0
for i in range(n):
if i and inp1[i] < inp2[i - 1]:
cur = 1
else:
cur += 1
ans = max(ans, cur)
stdout.write(f'{n - ans}\n')
main()
``` | output | 1 | 43,936 | 12 | 87,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,937 | 12 | 87,874 |
Tags: dp, greedy, two pointers
Correct Solution:
```
import sys
input=sys.stdin.buffer.readline
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
stored=[-1 for i in range(n+2)]
counted=[[0,0] for i in range(n+2)]
count=0
for i in range(n):
stored[a[i]]=a[i]
if(counted[a[i]][0]==0):
count+=1
counted[a[i]][0]+=1
counted[a[i]][1]+=1
pt=n+1
p_pt=0
for i in range(n+1,-1,-1):
if(stored[i]>=0):
p_pt=pt
pt=stored[i]
stored[i]=p_pt
else:
stored[i]=pt
ans=[0 for i in range(n+2)]
for i in range(n):
counted[a[i]][1] -= 1
if(counted[stored[a[i]]][0]-counted[stored[a[i]]][1]==0 and counted[a[i]][1]==0):
ans[stored[a[i]]]=ans[a[i]]+1
maxi=max(max(ans[:n+1])+1,ans[-1])
print(count-maxi)
``` | output | 1 | 43,937 | 12 | 87,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning. | instruction | 0 | 43,938 | 12 | 87,876 |
Tags: dp, greedy, two pointers
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
def ctd(chr): return ord(chr)-ord("a")
mod = 998244353
INF = float('inf')
from bisect import bisect_left
# ------------------------------
def main():
for _ in range(N()):
n = N()
arr = RLL()
dic = defaultdict(list)
for i in range(n): dic[arr[i]].append(i)
nl = list(dic.keys())
nl.sort(reverse=1)
le = len(nl)
dp = [1]*le
for i in range(1, le):
if dic[nl[i]][-1]<dic[nl[i-1]][0]:
dp[i] = dp[i-1]+1
print(le-max(dp))
if __name__ == "__main__":
main()
``` | output | 1 | 43,938 | 12 | 87,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning.
Submitted Solution:
```
from sys import stdin
input = stdin.readline
def Input():
global A, n, D ,F
n = int(input())
A = list(map(int, input().split()))
D = sorted(list(set(A)))
F = [0] * (n + 1)
def Ans():
for i in range(1, len(D) + 1):
F[D[i - 1]] = i
for i in range(n):
A[i] = F[A[i]]
m = len(D)
Start = [-1] * (m + 1)
End = [n + 1] * (m + 1)
for i in range(n):
if Start[A[i]] == -1:
Start[A[i]] = i
End[A[i]] = i
S = [0] * (m + 1)
Max = -float('inf')
for i in range(1, m + 1):
S[i] = S[i - 1] * (End[i - 1] < Start[i]) + 1
Max = max(Max, S[i])
Result.append(str(m-Max))
def main():
global Result
Result=[]
for _ in range(int(input())):
Input()
Ans()
print('\n'.join(Result))
if __name__ == '__main__':
main()
``` | instruction | 0 | 43,939 | 12 | 87,878 |
Yes | output | 1 | 43,939 | 12 | 87,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning.
Submitted Solution:
```
import copy
def DeleteRepetitionsIn(Array):
AlreadyRead = {}
index = 0
ConstantArray = copy.deepcopy(Array)
for a in range(len(ConstantArray)):
if Array[index] not in AlreadyRead:
AlreadyRead[Array[index]] = ""
index += 1
continue
Array = Array[0:index] + Array[index + 1:len(Array)]
return Array
def DeleteRepetitionsIn2(Array):
AlreadyRead = {}
for elem in Array:
if elem in AlreadyRead:
continue
AlreadyRead[elem] = ""
return list(AlreadyRead)
Results = []
ArraysNumber = int(input())
for e in range(ArraysNumber):
AbsolutelyUselessNumber = int(input())
Array = list(map(int, input().split()))
if len(Array) == 1:
Results.append(0)
continue
#print(Array)
TheRightOrder = DeleteRepetitionsIn2(Array)
TheRightOrder.sort()
TheCurrentOrder = {}
for i in range(len(Array)):
if Array[i] not in TheCurrentOrder:
TheCurrentOrder[Array[i]] = [i, i]
continue
TheCurrentOrder[Array[i]][1] = i
#print(TheRightOrder)
#print(TheCurrentOrder)
#print(Array)
TheCurrentResult = 1
TheMaxResult = 1
for i in range(len(TheRightOrder)):
#print("a =", TheCurrentResult)
#print("b =", TheMaxResult)
if i == len(TheRightOrder) - 1:
if TheCurrentResult >= TheMaxResult:
TheMaxResult = TheCurrentResult
continue
if TheCurrentOrder[TheRightOrder[i]][1] > TheCurrentOrder[TheRightOrder[i + 1]][0]:
if TheCurrentResult >= TheMaxResult:
TheMaxResult = TheCurrentResult
TheCurrentResult = 1
continue
TheCurrentResult += 1
Results.append(len(TheRightOrder) - TheMaxResult)
for i in Results:
print(i)
``` | instruction | 0 | 43,940 | 12 | 87,880 |
Yes | output | 1 | 43,940 | 12 | 87,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning.
Submitted Solution:
```
import sys
input = sys.stdin.readline
q=int(input())
for testcases in range(q):
n=int(input())
A=list(map(int,input().split()))
compression_dict={a: ind for ind, a in enumerate(sorted(set(A)))}
A=[compression_dict[a] for a in A]
MAX=max(A)
miind=[10**6]*(MAX+1)
maind=[0]*(MAX+1)
for i,a in enumerate(A):
miind[a]=min(i,miind[a])
maind[a]=max(i,maind[a])
#print(miind)
#print(maind)
ANS=0
con=0
for i in range(MAX):
if maind[i]<miind[i+1]:
con+=1
ANS=max(ANS,con)
else:
con=0
print(MAX-ANS)
``` | instruction | 0 | 43,941 | 12 | 87,882 |
Yes | output | 1 | 43,941 | 12 | 87,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning.
Submitted Solution:
```
q=int(input())
for i in range(q):
n=int(input())
l=list(map(int,input().split()))
lis=[0 for _ in range(n)]
for i in range(n):
lis[i] = 1
for j in range(i):
if l[i]<l[j]:
lis[i] = max(lis[i], lis[j]+1)
#print(lis)
print(max(0,max(lis)-1))
``` | instruction | 0 | 43,942 | 12 | 87,884 |
No | output | 1 | 43,942 | 12 | 87,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning.
Submitted Solution:
```
# from sys import stdin
#
# input = stdin.readline
#
#
# def main():
# anses = []
# for _ in range(int(input())):
# n = int(input())
# a = list(map(int, input().split()))
# f = [0] * (n + 1)
# d = sorted(list(set(a)))
# for q in range(1, len(d) + 1):
# f[d[q - 1]] = q
# for q in range(len(a)):
# a[q] = f[a[q]]
# n = len(d)
# starts, ends = [-1] * (n + 1), [n + 1] * (n + 1)
# for q in range(len(a)):
# if starts[a[q]] == -1:
# starts[a[q]] = q
# ends[a[q]] = q
# s = [0] * (n + 1)
# max1 = -float('inf')
# for q in range(1, n + 1):
# s[q] = s[q - 1] * (ends[q - 1] < starts[q]) + 1
# max1 = max(max1, s[q])
# anses.append(str(len(d) - max1))
# print('\n'.join(anses))
#
#
# main()
def vt(mn):
c = []
for i in a:
if i != mn:
c.append(i)
return c
def vt2(mn, mx):
c = []
for i in a:
if i != mn and i != mx:
c.append(i)
return c
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
s = sorted(a)
# por = [0] * (n + 1)
# for i in range(len(s)):
# por[s[i]] = i + 1
# for i in range(len(a)):
# a[i] = por[a[i]]
#
if a == s:
print(0)
elif [min(a)] * a.count(min(a)) + vt(min(a)) == s:
print(1)
elif [min(a)] * a.count((min(a))) + vt2(min(a), max(a)) + [max(a)] * a.count(max(a)) == s:
print(2)
``` | instruction | 0 | 43,943 | 12 | 87,886 |
No | output | 1 | 43,943 | 12 | 87,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
def ctd(chr): return ord(chr)-ord("a")
mod = 998244353
INF = float('inf')
from bisect import bisect_left
# ------------------------------
def main():
for _ in range(N()):
n = N()
arr = RLL()
dic = defaultdict(list)
for i in range(n): dic[arr[i]].append(i)
nl = list(dic.keys())
le = len(nl)
dp = [1]*le
for i in range(le):
for j in range(i-1, -1, -1):
if dic[nl[i]][0]>dic[nl[j]][-1]:
dp[i] = max(dp[j]+1, dp[i])
print(le-max(dp))
if __name__ == "__main__":
main()
``` | instruction | 0 | 43,944 | 12 | 87,888 |
No | output | 1 | 43,944 | 12 | 87,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n, consisting of integers.
You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.
For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):
* [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
* [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
* [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
* [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
* [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
* [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;
You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} β€ a_i is satisfied.
Note that you have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of the queries. Each query is represented by two consecutive lines.
The first line of each query contains one integer n (1 β€ n β€ 3 β
10^5) β the number of elements.
The second line of each query contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ n) β the elements.
It is guaranteed that the sum of all n does not exceed 3 β
10^5.
Output
For each query print one integer β the minimum number of operation for sorting sequence a in non-descending order.
Example
Input
3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7
Output
2
0
1
Note
In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.
In the second query, the sequence is sorted initially, so the answer is zero.
In the third query, you have to move all 2-elements to the beginning.
Submitted Solution:
```
q = int(input())
for i in range(q):
n = int(input())
nums = list(map(int, input().split()))
nums2 = sorted(set(nums))
arr1 = set()
m1 = nums[0]
for j in nums:
if j < m1:
arr1.add(j)
elif j > m1:
m1 = j
arr2 = set()
m2 = nums[-1]
for j in reversed(nums):
if j > m2:
arr2.add(j)
elif j < m2:
m2 = j
print(min(nums2.index(max(arr1)) + 1 if arr1 else 0,
len(nums2) - nums2.index(min(arr2)) if arr2 else 0))
``` | instruction | 0 | 43,945 | 12 | 87,890 |
No | output | 1 | 43,945 | 12 | 87,891 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,010 | 12 | 88,020 |
Tags: greedy, math
Correct Solution:
```
t=int(input())
ans=[]
for _ in range(t):
n=int(input())
l=list(map(int,input().split()))
flag=0
ce=0
co=0
for i in l[::2]:
if(i%2==1):
ce+=1
for i in l[1::2]:
if(i%2==0):
co+=1
if(ce!=co):
ans.append(str(-1))
else:
ans.append(str(ce))
print('\n'.join(ans))
``` | output | 1 | 44,010 | 12 | 88,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,011 | 12 | 88,022 |
Tags: greedy, math
Correct Solution:
```
t = int(input())
N = [None]*t
for k in range(t):
n = int(input())
m = list(map(int, input().split()))
bad_odd = 0
bad_even = 0
for i in range(len(m)):
if i % 2 != m[i] % 2 and i % 2 == 1:
bad_odd += 1
elif i % 2 != m[i] % 2 and i % 2 == 0:
bad_even += 1
if bad_odd == bad_even:
N[k] = bad_odd
else:
N[k] = -1
for k in range(t):
print(N[k])
``` | output | 1 | 44,011 | 12 | 88,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,012 | 12 | 88,024 |
Tags: greedy, math
Correct Solution:
```
from sys import stdin
def input(): return stdin.readline().rstrip()
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
c = 0
d = 0
for i in range(len(a)):
if a[i]%2 != i%2:
if i%2 == 0:
c += 1
else:
d += 1
if c != d:
print(-1)
else:
print(c)
``` | output | 1 | 44,012 | 12 | 88,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,013 | 12 | 88,026 |
Tags: greedy, math
Correct Solution:
```
for _ in range(int(input())):
m=int(input())
arr=list(map(int,input().split()))
n=[]
for k in range(m):
if(k%2==0):
if(arr[k]%2==0):
n.append(0)
else:
n.append('e')
else:
if(arr[k]%2!=0):
n.append(0)
else:
n.append('o')
p=n.count('e')
q=n.count('o')
if(p!=q):
print(-1)
else:
print(p)
``` | output | 1 | 44,013 | 12 | 88,027 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,014 | 12 | 88,028 |
Tags: greedy, math
Correct Solution:
```
def solve(arr,n,ans):
moves = 0
for i in range(n):
if arr[i]%2 != i%2:
index = -1
for j in range(i,n):
if arr[j]%2 != j%2 and arr[i]%2 == j%2 and arr[j]%2 == i%2:
index = j
break
if index == -1:
ans.append(-1)
return
arr[i],arr[j] = arr[j],arr[i]
moves += 1
ans.append(moves)
def main():
t = int(input())
ans = []
for i in range(t):
n = int(input())
arr = list(map(int,input().split()))
solve(arr,n,ans)
for i in ans:
print(i)
main()
``` | output | 1 | 44,014 | 12 | 88,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,015 | 12 | 88,030 |
Tags: greedy, math
Correct Solution:
```
for _ in range(int(input())):
x=int(input())
a=list(map(int, input().split()))
b=[]
for i in range(x):
if a[i]%2!=i%2 and i%2==1:
b.append(1)
elif a[i]%2!=i%2 and i%2==0:
b.append(0)
if b.count(0)==b.count(1): print(b.count(0))
else: print(-1)
``` | output | 1 | 44,015 | 12 | 88,031 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,016 | 12 | 88,032 |
Tags: greedy, math
Correct Solution:
```
#!/bin/python3
#https://codeforces.com/problemset/problem/1367/B
import sys, os.path, math
pwd = os.path.dirname(__file__)
if(os.path.exists(os.path.join(pwd,'../iostream/input.txt'))):
sys.stdin = open(os.path.join(pwd,'../iostream/input.txt'), 'r')
sys.stdout = open(os.path.join(pwd,'../iostream/output.txt'), 'w')
def solve():
for i in range(int(input())):
length = int(input());
array = [int(j) for j in input().split()]
odd = []
even = []
for j in range(length):
if (j&1) != array[j]&1:
if array[j]&1: odd.append(array[j])
else: even.append(array[j])
if len(odd) == len(even):print(len(odd))
else: print(-1)
if __name__ == '__main__':
solve()
``` | output | 1 | 44,016 | 12 | 88,033 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good. | instruction | 0 | 44,017 | 12 | 88,034 |
Tags: greedy, math
Correct Solution:
```
from fractions import Fraction
from collections import defaultdict
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
#----------------------------------------Code Starts Here--------------------------------------------#
t = int(input())
for _ in range(t):
n = int(input())
l = list([int(x) for x in input().split()])
d = {0: 0, 1: 0}
c = 0
for i in range(n):
if((i % 2) != (l[i] % 2)):
c += 1
if(l[i] % 2 == 0):
d[0] += 1
else:
d[1] += 1
if(n % 2 == 0):
if(d[0] == d[1] and c % 2 == 0):
print(c//2)
else:
print('-1')
else:
if(d[0] == d[1]+1 and c % 2 == 0):
print(c//2)
else:
print('-1')
``` | output | 1 | 44,017 | 12 | 88,035 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
import math
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
x=0
y=0
z=0
w=0
for j in range(n):
if a[j]%2==0:
x+=1
if j%2!=0:
z+=1
else:
y+=1
if j%2==0:
w+=1
if n%2==0 and x!=y:
print(-1)
elif n%2!=0 and x!=y+1:
print(-1)
elif z==w:
print(z)
else:
print(-1)
``` | instruction | 0 | 44,018 | 12 | 88,036 |
Yes | output | 1 | 44,018 | 12 | 88,037 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
T=int(input())
B=[]
for k in range(T):
N=int(input())
A=list(map(int,input().split()))
b=0
c=0
for i in range(N):
if i%2==0:
if A[i]%2==0:
continue
else:
b+=1
elif i%2==1:
if A[i]%2==1:
continue
else:
c+=1
if b==c:
B.append(b)
else:
B.append(-1)
for i in B:
print(i)
``` | instruction | 0 | 44,019 | 12 | 88,038 |
Yes | output | 1 | 44,019 | 12 | 88,039 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
swap = swap2 = 0
# if n % 2 == 0:
# odd, even = n//2, n//2
# else:
# odd, even = n//2, (n//2)+1
for i in range(n):
if i % 2 == 1:
if a[i] % 2 == 0:
swap += 1
else:
if a[i] % 2 == 1:
swap2 += 1
if swap == swap2:
print(swap)
else:
print(-1)
main()
``` | instruction | 0 | 44,020 | 12 | 88,040 |
Yes | output | 1 | 44,020 | 12 | 88,041 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
l = [int(x) for x in input().split()]
parity = [True if elm % 2 == 0 else False for elm in l]
if parity.count(False) != n//2:
print(-1)
else:
print([True if elm % 2 == ind % 2 else False for ind,elm in enumerate(l)].count(False) // 2)
``` | instruction | 0 | 44,021 | 12 | 88,042 |
Yes | output | 1 | 44,021 | 12 | 88,043 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
import sys
get = sys.stdin.readline
tc = int(get())
for _ in range(tc):
n = int(get())
arr = list(map(int, get().split()))
ans = 0
check = 0
for i in range(n):
if arr[i]%2 == 0 and i%2 == 1:
check += 1
if arr[i]%2 == 1 and i%2 == 0:
check += 1
if check%2 == 0:
ans = check//2
else:
ans = -1
print(ans)
``` | instruction | 0 | 44,022 | 12 | 88,044 |
No | output | 1 | 44,022 | 12 | 88,045 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
t=int(input())
a=[]
for i in range(t):
n = int(input())
s = [int(i) for i in input().split()]
num = None
ans = 0
for j in range(n):
if j % 2 != s[j] % 2:
if num != None:
s[j], s[num] = s[num], s[j]
num = None
ans += 1
else:
num = j
if num != None:
a.append(-1)
else:
a.append(ans)
for i in a: print(i)
``` | instruction | 0 | 44,023 | 12 | 88,046 |
No | output | 1 | 44,023 | 12 | 88,047 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
t = int(input())
while t:
t-=1
n = int(input())
s = list(map(int,input().split()))
No = False
move =0
good = True
if(n==1):
if(s[0]%2 == 0):
print(0)
else:
print(-1)
continue
for i in range(n):
if(s[i]%2 == i%2):
continue
else:
if(i==n-1):
No=True
break
good = False
need = i%2
for y in range(i+1,len(s),2):
if need == s[y]%2:
temp = s[y]
s[y] = s[i]
s[i] = temp
move+=1
break
if y==len(s)-1:
No = True
if No == True:
break
if good:
print(0)
continue
if No:
print(-1)
continue
if good==False and move==0:
print(-1)
continue
print(move)
``` | instruction | 0 | 44,024 | 12 | 88,048 |
No | output | 1 | 44,024 | 12 | 88,049 |
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[0 β¦ n-1] of length n which consists of non-negative integers. Note that array indices start from zero.
An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all i (0 β€ i β€ n - 1) the equality i mod 2 = a[i] mod 2 holds, where x mod 2 is the remainder of dividing x by 2.
For example, the arrays [0, 5, 2, 1] and [0, 17, 0, 3] are good, and the array [2, 4, 6, 7] is bad, because for i=1, the parities of i and a[i] are different: i mod 2 = 1 mod 2 = 1, but a[i] mod 2 = 4 mod 2 = 0.
In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).
Find the minimum number of moves in which you can make the array a good, or say that this is not possible.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases in the test. Then t test cases follow.
Each test case starts with a line containing an integer n (1 β€ n β€ 40) β the length of the array a.
The next line contains n integers a_0, a_1, β¦, a_{n-1} (0 β€ a_i β€ 1000) β the initial array.
Output
For each test case, output a single integer β the minimum number of moves to make the given array a good, or -1 if this is not possible.
Example
Input
4
4
3 2 7 6
3
3 2 6
1
7
7
4 9 2 1 18 3 0
Output
2
1
-1
0
Note
In the first test case, in the first move, you can swap the elements with indices 0 and 1, and in the second move, you can swap the elements with indices 2 and 3.
In the second test case, in the first move, you need to swap the elements with indices 0 and 1.
In the third test case, you cannot make the array good.
Submitted Solution:
```
def foo(sizeOfArray, l):
countOdd =0
countEven = 0
for i in range(0,sizeOfArray,2):
if l[i]%2==1: countOdd = countOdd+1
for i in range(1,sizeOfArray,2):
if l[i]%2==0: countEven = countEven+1
if countEven>=countOdd: return countOdd
if countOdd>countEven: return -1
if __name__ == '__main__':
n = input()
n = int(n)
answers = list()
for i in range(n):
sizeOfArray = input()
sizeOfArray = int(sizeOfArray)
l = [int(x) for x in input().split()]
answers.append(foo(sizeOfArray, l))
for i in answers:
print(i)
``` | instruction | 0 | 44,025 | 12 | 88,050 |
No | output | 1 | 44,025 | 12 | 88,051 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
There is a secret permutation p (1-indexed) of numbers from 1 to n. More formally, for 1 β€ i β€ n, 1 β€ p[i] β€ n and for 1 β€ i < j β€ n, p[i] β p[j]. It is known that p[1]<p[2].
In 1 query, you give 3 distinct integers a,b,c (1 β€ a,b,c β€ n), and receive the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In this case, the median is the 2-nd element (1-indexed) of the sequence when sorted in non-decreasing order. The median of \{4,6,2\} is 4 and the median of \{0,123,33\} is 33.
Can you find the secret permutation in not more than 2n+420 queries?
Note: the grader is not adaptive: the permutation is fixed before any queries are made.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase consists of a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
It is guaranteed that the sum of n over all test cases does not exceed 100000.
Interaction
For each testcase, you begin the interaction by reading n.
To perform a query, output "? a b c" where a,b,c is the 3 indices you want to use for the query.
Numbers have to satisfy 1 β€ a,b,c β€ n and a β b,b β c,a β c.
For each query, you will receive a single integer x: the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In case your query is invalid or you asked more than 2n+420 queries, the interactor will print "β1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you have determined the secret permutation, output "! p[1] p[2] ... p[n]". If the secret permutation is correct, the interactor will print "1". Otherwise, the interactor will print "-1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict.
To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks:
To hack, use the following format of test:
The first line should contain a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase should contain a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
The following line of should contain n integers p[1],p[2],p[3],β¦,p[n]. p[1]<p[2] and p must be a permutation of integers from 1 to n.
You must ensure that the sum of n over all testcases does not exceed 100000.
Example
Input
1
20
6
9
1
Output
? 1 5 2
? 20 19 2
! 9 10 19 7 16 18 11 14 15 6 20 8 17 4 5 3 12 2 13 1
Note
The secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}.
For the first query, the values of (a,b,c) is (1,5,2). Since p[1]=9, p[5]=16 and p[2]=10. The return value is the median of \{|9-16|,|16-10|,|9-10|\} which is 6.
For the second query, the values of (a,b,c) is (20,19,2). Since p[20]=1, p[19]=13 and p[2]=10. The return value is the median of \{|1-13|,|13-10|,|1-10|\} which is 9.
By some miracle, we have figured out that the secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}. We output it and receive 1 from the interactor, meaning that we have guessed the secret permutation correctly.
Submitted Solution:
```
# cook your dish here
no_elements_array = int(input())
arr = list(map(int, input().split()))
stack = []
for i in arr:
if stack ==[]:
stack.insert(0,i)
else:
if stack[0]==i:
stack.pop(0)
else:
stack.insert(0,i)
print(len(stack))
``` | instruction | 0 | 44,072 | 12 | 88,144 |
No | output | 1 | 44,072 | 12 | 88,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
There is a secret permutation p (1-indexed) of numbers from 1 to n. More formally, for 1 β€ i β€ n, 1 β€ p[i] β€ n and for 1 β€ i < j β€ n, p[i] β p[j]. It is known that p[1]<p[2].
In 1 query, you give 3 distinct integers a,b,c (1 β€ a,b,c β€ n), and receive the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In this case, the median is the 2-nd element (1-indexed) of the sequence when sorted in non-decreasing order. The median of \{4,6,2\} is 4 and the median of \{0,123,33\} is 33.
Can you find the secret permutation in not more than 2n+420 queries?
Note: the grader is not adaptive: the permutation is fixed before any queries are made.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase consists of a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
It is guaranteed that the sum of n over all test cases does not exceed 100000.
Interaction
For each testcase, you begin the interaction by reading n.
To perform a query, output "? a b c" where a,b,c is the 3 indices you want to use for the query.
Numbers have to satisfy 1 β€ a,b,c β€ n and a β b,b β c,a β c.
For each query, you will receive a single integer x: the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In case your query is invalid or you asked more than 2n+420 queries, the interactor will print "β1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you have determined the secret permutation, output "! p[1] p[2] ... p[n]". If the secret permutation is correct, the interactor will print "1". Otherwise, the interactor will print "-1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict.
To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks:
To hack, use the following format of test:
The first line should contain a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase should contain a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
The following line of should contain n integers p[1],p[2],p[3],β¦,p[n]. p[1]<p[2] and p must be a permutation of integers from 1 to n.
You must ensure that the sum of n over all testcases does not exceed 100000.
Example
Input
1
20
6
9
1
Output
? 1 5 2
? 20 19 2
! 9 10 19 7 16 18 11 14 15 6 20 8 17 4 5 3 12 2 13 1
Note
The secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}.
For the first query, the values of (a,b,c) is (1,5,2). Since p[1]=9, p[5]=16 and p[2]=10. The return value is the median of \{|9-16|,|16-10|,|9-10|\} which is 6.
For the second query, the values of (a,b,c) is (20,19,2). Since p[20]=1, p[19]=13 and p[2]=10. The return value is the median of \{|1-13|,|13-10|,|1-10|\} which is 9.
By some miracle, we have figured out that the secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}. We output it and receive 1 from the interactor, meaning that we have guessed the secret permutation correctly.
Submitted Solution:
```
from random import randint
T = 400
def query(a, b, c):
print('?', a + 1, b + 1, c + 1, flush=True)
return int(input())
t = int(input())
while t > 0:
t -= 1
n = int(input())
cer: tuple
best = n
for i in range(T):
a, b, c = randint(0, n - 1), randint(0, n - 2), randint(0, n - 3)
b += b >= a
c += c >= a
c += c >= b
ret = query(a, b, c)
if ret < best:
best = ret
cer = a, b
a, b = cer
distances = []
for i in range(n):
if a != i and a != b:
distances += [query(a, b, i), i]
distances.sort(reverse=True)
far = distances[0][1]
second_far = min(filter(lambda e: e[0] == distances[0][0] - 1, distances), key=lambda e: query(a, far, e[0]))[0]
ans = [0] * n
ans[far] = 0
ans[second_far] = 1
for i in range(n):
if i != far and i != second_far:
ans[i] = query(far, second_far, i) + 1
if ans[0] > ans[1]:
ans = [n - x - 1 for x in ans]
print(x + 1 for x in ans)
``` | instruction | 0 | 44,073 | 12 | 88,146 |
No | output | 1 | 44,073 | 12 | 88,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
There is a secret permutation p (1-indexed) of numbers from 1 to n. More formally, for 1 β€ i β€ n, 1 β€ p[i] β€ n and for 1 β€ i < j β€ n, p[i] β p[j]. It is known that p[1]<p[2].
In 1 query, you give 3 distinct integers a,b,c (1 β€ a,b,c β€ n), and receive the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In this case, the median is the 2-nd element (1-indexed) of the sequence when sorted in non-decreasing order. The median of \{4,6,2\} is 4 and the median of \{0,123,33\} is 33.
Can you find the secret permutation in not more than 2n+420 queries?
Note: the grader is not adaptive: the permutation is fixed before any queries are made.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase consists of a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
It is guaranteed that the sum of n over all test cases does not exceed 100000.
Interaction
For each testcase, you begin the interaction by reading n.
To perform a query, output "? a b c" where a,b,c is the 3 indices you want to use for the query.
Numbers have to satisfy 1 β€ a,b,c β€ n and a β b,b β c,a β c.
For each query, you will receive a single integer x: the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In case your query is invalid or you asked more than 2n+420 queries, the interactor will print "β1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you have determined the secret permutation, output "! p[1] p[2] ... p[n]". If the secret permutation is correct, the interactor will print "1". Otherwise, the interactor will print "-1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict.
To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks:
To hack, use the following format of test:
The first line should contain a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase should contain a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
The following line of should contain n integers p[1],p[2],p[3],β¦,p[n]. p[1]<p[2] and p must be a permutation of integers from 1 to n.
You must ensure that the sum of n over all testcases does not exceed 100000.
Example
Input
1
20
6
9
1
Output
? 1 5 2
? 20 19 2
! 9 10 19 7 16 18 11 14 15 6 20 8 17 4 5 3 12 2 13 1
Note
The secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}.
For the first query, the values of (a,b,c) is (1,5,2). Since p[1]=9, p[5]=16 and p[2]=10. The return value is the median of \{|9-16|,|16-10|,|9-10|\} which is 6.
For the second query, the values of (a,b,c) is (20,19,2). Since p[20]=1, p[19]=13 and p[2]=10. The return value is the median of \{|1-13|,|13-10|,|1-10|\} which is 9.
By some miracle, we have figured out that the secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}. We output it and receive 1 from the interactor, meaning that we have guessed the secret permutation correctly.
Submitted Solution:
```
from random import randint
T = 400
def query(a, b, c):
print('?', a + 1, b + 1, c + 1, flush=True)
return int(input())
t = int(input())
while t > 0:
t -= 1
n = int(input())
cer: tuple
best = n
for i in range(T):
a, b, c = sorted([randint(0, n - 1), randint(0, n - 2), randint(0, n - 3)])
b += b >= a
c += c >= a
c += c >= b
ret = query(a, b, c)
if ret < best:
best = ret
cer = a, b
a, b = cer
distances = []
for i in range(n):
if a != i and a != b:
distances += [query(a, b, i), i]
distances.sort(reverse=True)
far = distances[0][1]
second_far = min(filter(lambda e: e[0] == distances[0][0] - 1, distances), key=lambda e: query(a, far, e[0]))[0]
ans = [0] * n
ans[far] = 0
ans[second_far] = 1
for i in range(n):
if i != far and i != second_far:
ans[i] = query(far, second_far, i) + 1
if ans[0] > ans[1]:
ans = [n - x - 1 for x in ans]
print(x + 1 for x in ans)
``` | instruction | 0 | 44,074 | 12 | 88,148 |
No | output | 1 | 44,074 | 12 | 88,149 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
There is a secret permutation p (1-indexed) of numbers from 1 to n. More formally, for 1 β€ i β€ n, 1 β€ p[i] β€ n and for 1 β€ i < j β€ n, p[i] β p[j]. It is known that p[1]<p[2].
In 1 query, you give 3 distinct integers a,b,c (1 β€ a,b,c β€ n), and receive the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In this case, the median is the 2-nd element (1-indexed) of the sequence when sorted in non-decreasing order. The median of \{4,6,2\} is 4 and the median of \{0,123,33\} is 33.
Can you find the secret permutation in not more than 2n+420 queries?
Note: the grader is not adaptive: the permutation is fixed before any queries are made.
Input
The first line of input contains a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase consists of a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
It is guaranteed that the sum of n over all test cases does not exceed 100000.
Interaction
For each testcase, you begin the interaction by reading n.
To perform a query, output "? a b c" where a,b,c is the 3 indices you want to use for the query.
Numbers have to satisfy 1 β€ a,b,c β€ n and a β b,b β c,a β c.
For each query, you will receive a single integer x: the median of \{|p[a]-p[b]|,|p[b]-p[c]|,|p[a]-p[c]|\}.
In case your query is invalid or you asked more than 2n+420 queries, the interactor will print "β1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
When you have determined the secret permutation, output "! p[1] p[2] ... p[n]". If the secret permutation is correct, the interactor will print "1". Otherwise, the interactor will print "-1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict.
To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks:
To hack, use the following format of test:
The first line should contain a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase should contain a single integer n (20 β€ n β€ 100000) β the length of the secret permutation.
The following line of should contain n integers p[1],p[2],p[3],β¦,p[n]. p[1]<p[2] and p must be a permutation of integers from 1 to n.
You must ensure that the sum of n over all testcases does not exceed 100000.
Example
Input
1
20
6
9
1
Output
? 1 5 2
? 20 19 2
! 9 10 19 7 16 18 11 14 15 6 20 8 17 4 5 3 12 2 13 1
Note
The secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}.
For the first query, the values of (a,b,c) is (1,5,2). Since p[1]=9, p[5]=16 and p[2]=10. The return value is the median of \{|9-16|,|16-10|,|9-10|\} which is 6.
For the second query, the values of (a,b,c) is (20,19,2). Since p[20]=1, p[19]=13 and p[2]=10. The return value is the median of \{|1-13|,|13-10|,|1-10|\} which is 9.
By some miracle, we have figured out that the secret permutation is \{9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1\}. We output it and receive 1 from the interactor, meaning that we have guessed the secret permutation correctly.
Submitted Solution:
```
import os
from random import randint
from sys import stdout, stdin
input = stdin.buffer.readline
T = 400
def query(a, b, c):
os.write(1, b"%d %d %d\n" % (a + 1, b + 1, c + 1))
ret = int(input())
if ret == -1:
quit()
return ret
t = int(input())
while t > 0:
t -= 1
n = int(input())
cer: tuple
best = n
for i in range(T):
a, b, c = randint(0, n - 1), randint(0, n - 2), randint(0, n - 3)
b += b >= a
if b < a:
a, b = b, a
c += c >= a
c += c >= b
ret = query(a, b, c)
if ret < best:
best = ret
cer = a, b
a, b = cer
distances = []
for i in range(n):
if a != i and b != i:
distances += [[query(a, b, i), i]]
far_dis, far = max(distances)
second_far = min(filter(lambda e: e[0] == far_dis - 1, distances), key=lambda e: query(a, far, e[1]))[1]
# print(a, b, file=stderr)
# print(distances, file=stderr)
# print(list(filter(lambda e: e[0] == distances[0][0] - 1, distances)), file=stderr)
# print(far, second_far, file=stderr)
ans = [0] * n
ans[far] = 0
ans[second_far] = 1
for i in range(n):
if i != far and i != second_far:
ans[i] = query(far, second_far, i) + 1
if ans[0] > ans[1]:
ans = [n - x - 1 for x in ans]
print('!', *(x + 1 for x in ans))
stdout.flush()
if input() != '1':
quit(2)
``` | instruction | 0 | 44,075 | 12 | 88,150 |
No | output | 1 | 44,075 | 12 | 88,151 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,157 | 12 | 88,314 |
Tags: data structures, dp
Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n,m=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
b=[0]*(n+1)
s=set()
for i in range(n,0,-1):
s.add(a[i-1])
b[i]=str(len(s))
'''for i in range(m):
l=int(input())
print(b[l])'''
print('\n'.join([b[int(input())] for _ in range(m)]))
``` | output | 1 | 44,157 | 12 | 88,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,158 | 12 | 88,316 |
Tags: data structures, dp
Correct Solution:
```
n,m=map(int,input().split())
a=[int(x) for x in input().split()]
b=[0]*m
d=[0]*n
for i in range(m):
b[i]=int(input())
hx=[0]*100010
ans=0
for i in range(n):
if(hx[a[n-1-i]]==0):
hx[a[n-1-i]]=1
ans+=1
d[n-1-i]=str(int(ans))
for i in range(m):
print(d[b[i]-1])
'''
8 3
8 6 4 3 4 2 4 8
6
4
2'''
``` | output | 1 | 44,158 | 12 | 88,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,159 | 12 | 88,318 |
Tags: data structures, dp
Correct Solution:
```
n,m = [int(i) for i in input().split()]
nums = [int(i) for i in input().split()]
seen = set()
memo = [1]*n
res = ''
seen.add(nums[n-1])
for i in range(n-2,-1,-1):
if nums[i] not in seen:
seen.add(nums[i])
memo[i] = memo[i+1]+1
else:
memo[i] = memo[i+1]
for _ in range(m):
f = int(input())
res+=str(memo[f-1]) + '\n'
print(res)
``` | output | 1 | 44,159 | 12 | 88,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,160 | 12 | 88,320 |
Tags: data structures, dp
Correct Solution:
```
n,m = list(map(int, input().split()))
arr= list(map(int, input().split()))
visited=[False for _ in range (100005)]
dp=[0 for _ in range(n+1)]
dp[-1]=1
visited [arr[-1] ]=True
for i in range(n-1,0,-1):
# print(arr[i-1],visited[arr[i-1]])
if visited[arr[i-1]]:
dp[i]=dp[i+1]
else:
dp[i]=dp[i+1]+1
visited[arr[i-1]]=True
#print(*arr)
#print(*dp)
for _ in range(m):
q = int(input())
print(dp[q])
``` | output | 1 | 44,160 | 12 | 88,321 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,161 | 12 | 88,322 |
Tags: data structures, dp
Correct Solution:
```
n, m = [int(x) for x in input().strip().split()]
a = [int(x) for x in input().strip().split()]
used=set([a[-1]])
cnt=[1]*(n+1)
for i in range(n-2,-1,-1):
if(a[i] in used):
cnt[i+1] = cnt[i+2]
else:
cnt[i+1] = cnt[i+2] + 1
used.add(a[i])
#print(cnt)
for i in range(m):
temp = int(input().strip())
print(cnt[temp])
``` | output | 1 | 44,161 | 12 | 88,323 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,162 | 12 | 88,324 |
Tags: data structures, dp
Correct Solution:
```
cnt = lambda s, i: s.count(i)
ii = lambda: int(input())
si = lambda : input()
f = lambda : map(int,input().split())
il = lambda : list(map(int,input().split()))
n,m=f()
l=il()
z=set()
for i in range(n-1,-1,-1):
z.add(l[i])
l[i]=len(z)
print('\n'.join(str(l[ii()-1]) for _ in range(m)))
``` | output | 1 | 44,162 | 12 | 88,325 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,163 | 12 | 88,326 |
Tags: data structures, dp
Correct Solution:
```
n, m=map(int, input().split(' '))
array=input().split(' ')
listx=[int(input()) for inpt in range(m)]
nums=set()
for i in range(n-1, -1, -1):
nums.add(array[i])
array[i]=len(nums)
for a in range(m):
print(array[listx[a]-1])
``` | output | 1 | 44,163 | 12 | 88,327 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 β€ li β€ n). For each number li he wants to know how many distinct numbers are staying on the positions li, li + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?
Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.
Input
The first line contains two integers n and m (1 β€ n, m β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the array elements.
Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 β€ li β€ n).
Output
Print m lines β on the i-th line print the answer to the number li.
Examples
Input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
Output
6
6
6
6
6
5
4
3
2
1 | instruction | 0 | 44,164 | 12 | 88,328 |
Tags: data structures, dp
Correct Solution:
```
import sys
input=sys.stdin.readline
n ,m = map(int,input().split())
a = list(map(int,input().split()))
dp = a
Data = set()
for i in range(n-1,-1,-1):
Data.add(a[i])
dp[i] = len(Data)
#print(dp)
for i in range(m):
print(dp[int(input())-1])
#hmachenan ba fekr khande hat roza mishan moror :)
``` | output | 1 | 44,164 | 12 | 88,329 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.