message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6 | instruction | 0 | 106,343 | 12 | 212,686 |
Tags: binary search, data structures, two pointers
Correct Solution:
```
class SegmentTree:
def __init__(self, init_val, segfunc, ide_ele):
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
self.range = [(-1,n)] * 2 * self.num
# 配列の値を葉にセット
for i in range(n):
self.tree[self.num + i] = init_val[i]
self.range[self.num + i] = (i,i)
# 構築していく
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
self.range[i] = (self.range[2 * i][0],self.range[2 * i + 1][1])
def update(self, k, x):
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
def bisect_l(self,l,r,x):
l += self.num
r += self.num
Lmin = -1
Rmin = -1
while l<r:
if l & 1:
if self.tree[l] <= x and Lmin==-1:
Lmin = l
l += 1
if r & 1:
if self.tree[r-1] <=x:
Rmin = r-1
l >>= 1
r >>= 1
if Lmin != -1:
pos = Lmin
while pos<self.num:
if self.tree[2 * pos] <=x:
pos = 2 * pos
else:
pos = 2 * pos +1
return pos-self.num
elif Rmin != -1:
pos = Rmin
while pos<self.num:
if self.tree[2 * pos] <=x:
pos = 2 * pos
else:
pos = 2 * pos +1
return pos-self.num
else:
return -1
n = int(input())
p = list(map(int,input().split()))
pos = [[] for i in range(n+2)]
for i in range(n):
pos[p[i]].append(i)
query = [[] for i in range(n)]
for i in range(1,n+2):
for j in range(len(pos[i])-1):
L = pos[i][j] + 1
R = pos[i][j+1] - 1
if L<=R:
query[R].append((L,i))
if pos[i]:
if pos[i][0]!=0:
query[pos[i][0]-1].append((0,i))
if pos[i][-1]!=n-1:
query[n-1].append((pos[i][-1]+1,i))
else:
query[n-1].append((0,i))
#print(query)
flag = [False for i in range(n+3)]
init = [-1]*(n+2)
init[0] = n
lastappeared = SegmentTree(init,min,-1)
for i in range(n):
lastappeared.update(p[i],i)
for l,val in query[i]:
check = lastappeared.bisect_l(0,n+2,l-1)
#print(l,i,val,check)
#pp = [lastappeared.tree[j+lastappeared.num] for j in range(n)]
if check>=val or check==-1:
flag[val] = True
for i in range(1,n+3):
if not flag[i]:
print(i)
break
``` | output | 1 | 106,343 | 12 | 212,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6 | instruction | 0 | 106,344 | 12 | 212,688 |
Tags: binary search, data structures, two pointers
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
ans = 1
last, pre = [0] * (n + 2), [n + 2] * (n + 2)
b = [n + 1] * (n + 1)
for i in range(1, n + 1):
pre[i] = last[a[i - 1]]
last[a[i - 1]] = i
def update(i, x):
while i <= n:
b[i] = min(b[i], x)
i += i & -i
def getmin(i):
ans = n + 2
while i:
ans = min(ans, b[i])
i -= i & -i
return ans
vis = [False] * (n + 3)
for i in range(1, n + 1):
update(i, last[i])
for i in range(1, n + 2):
if last[i] != n and getmin(i - 1) > last[i]:
vis[i] = True
for i in range(n, 0, -1):
update(a[i - 1], pre[i])
if pre[i] != i - 1 and getmin(a[i - 1] - 1) > pre[i]:
vis[a[i - 1]] = True
ans = 1
while vis[ans]:
ans += 1
print(ans)
``` | output | 1 | 106,344 | 12 | 212,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6 | instruction | 0 | 106,345 | 12 | 212,690 |
Tags: binary search, data structures, two pointers
Correct Solution:
```
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10
class SegTree:
def __init__(self, n, func, intv, A=[]):
self.n = n
self.func = func
self.intv = intv
n2 = 1
while n2 < n:
n2 <<= 1
self.n2 = n2
self.tree = [self.intv] * (n2 << 1)
if A:
for i in range(n):
self.tree[n2+i] = A[i]
for i in range(n2-1, -1, -1):
self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])
def update(self, i, x):
i += self.n2
self.tree[i] = x
while i > 0:
i >>= 1
self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])
def add(self, i, x):
self.update(i, self.get(i) + x)
def query(self, a, b):
l = a + self.n2
r = b + self.n2
s = self.intv
while l < r:
if r & 1:
r -= 1
s = self.func(s, self.tree[r])
if l & 1:
s = self.func(s, self.tree[l])
l += 1
l >>= 1
r >>= 1
return s
def get(self, i):
return self.tree[i+self.n2]
def all(self):
return self.tree[1]
def print(self):
for i in range(self.n):
print(self.get(i), end=' ')
print()
N = INT()
A = LIST()
seg = SegTree(N+2, min, INF, [-1]*(N+2))
mex = [0] * (N+2)
if sorted(A, reverse=1)[0] != 1:
mex[1] = 1
for i in range(N):
if A[i] == 1:
seg.update(A[i], i)
continue
prev = seg.get(A[i])
l = 1
r = A[i]
mn = seg.query(l, r)
if mn > prev:
mex[A[i]] = 1
seg.update(A[i], i)
for a in range(2, N+2):
prev = seg.get(a)
l = 1
r = a
mn = seg.query(l, r)
if mn > prev:
mex[a] = 1
ans = N + 2
for i in range(1, N+2):
if not mex[i]:
ans = i
break
print(ans)
``` | output | 1 | 106,345 | 12 | 212,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6 | instruction | 0 | 106,346 | 12 | 212,692 |
Tags: binary search, data structures, two pointers
Correct Solution:
```
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
#from decimal import Decimal
#from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7
class SegmentTree:
def __init__(self, data, default=0, func=min):
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
while idx:
idx >>= 1
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
def __len__(self):
return self._len
def query(self, start, stop):
"""func of data[start, stop)"""
start += self._size
stop += self._size
res_left = res_right = self._default
while start < stop:
if start & 1:
res_left = self._func(res_left, self.data[start])
start += 1
if stop & 1:
stop -= 1
res_right = self._func(self.data[stop], res_right)
start >>= 1
stop >>= 1
return self._func(res_left, res_right)
def __repr__(self):
return "SegmentTree({0})".format(self.data)
n=int(data())
a=mdata()
if len(set(a))==1 and 1 in a:
out(1)
exit()
S=SegmentTree(list(range(-n-1,0)),INF)
m=[0]*(n+1)
for i in range(n):
x=a[i]
ind=S.query(0,x)
if ind<0:
m[ind]=1
else:
m[a[ind]-1]=1
S.__setitem__(x-1,i)
for i in range(n+1):
ind = S.query(0,i+1)
if ind < 0:
m[ind] = 1
else:
m[a[ind] - 1] = 1
for i in range(n+1):
if m[i]==0:
out(i+1)
exit()
out(n+2)
``` | output | 1 | 106,346 | 12 | 212,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6 | instruction | 0 | 106,347 | 12 | 212,694 |
Tags: binary search, data structures, two pointers
Correct Solution:
```
class SegmentTree:
def __init__(self, init_val, segfunc, ide_ele):
n = len(init_val);self.segfunc = segfunc;self.ide_ele = ide_ele;self.num = 1 << (n - 1).bit_length();self.tree = [ide_ele] * 2 * self.num;self.range = [(-1,n)] * 2 * self.num
for i in range(n):self.tree[self.num + i] = init_val[i];self.range[self.num + i] = (i,i)
for i in range(self.num - 1, 0, -1):self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]);self.range[i] = (self.range[2 * i][0],self.range[2 * i + 1][1])
def update(self, k, x):
k += self.num;self.tree[k] = x
while k > 1:self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]);k >>= 1
def query(self, l, r):
res = self.ide_ele;l += self.num;r += self.num
while l < r:
if l & 1:res = self.segfunc(res, self.tree[l]);l += 1
if r & 1:res = self.segfunc(res, self.tree[r - 1]);l >>= 1
r >>= 1
return res
def bisect_l(self,l,r,x):
l += self.num;r += self.num;Lmin = -1;Rmin = -1
while l<r:
if l & 1:
if self.tree[l] <= x and Lmin==-1:Lmin = l
l += 1
if r & 1:
if self.tree[r-1] <=x:Rmin = r-1
l >>= 1;r >>= 1
if Lmin != -1:
pos = Lmin
while pos<self.num:pos = (2 * pos if self.tree[2 * pos] <= x else 2 * pos + 1)
return pos-self.num
elif Rmin != -1:
pos = Rmin
while pos<self.num:pos = (2*pos if self.tree[2 * pos] <=x else 2 * pos + 1)
return pos-self.num
else:return -1
n = int(input());p = list(map(int,input().split()));pos = [[] for i in range(n+2)];query = [[] for i in range(n)]
for i in range(n):pos[p[i]].append(i)
for i in range(1,n+2):
for j in range(len(pos[i])-1):
L = pos[i][j] + 1;R = pos[i][j+1] - 1
if L<=R:query[R].append((L,i))
if pos[i]:
if pos[i][0]!=0:query[pos[i][0]-1].append((0,i))
if pos[i][-1]!=n-1:query[n-1].append((pos[i][-1]+1,i))
else:query[n-1].append((0,i))
flag = [False for i in range(n+3)];init = [-1]*(n+2);init[0] = n;lastappeared = SegmentTree(init,min,-1)
for i in range(n):
lastappeared.update(p[i],i)
for l,val in query[i]:
check = lastappeared.bisect_l(0,n+2,l-1)
if check>=val or check==-1:flag[val] = True
for i in range(1,n+3):
if not flag[i]:print(i);break
``` | output | 1 | 106,347 | 12 | 212,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6 | instruction | 0 | 106,348 | 12 | 212,696 |
Tags: binary search, data structures, two pointers
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
mn = [-1 for _ in range(n*4)]
def Set(c, ss, se, qi, f):
if ss==se:
mn[c] = f
else:
mid = ss+se>>1
if qi<=mid:
Set(c*2, ss, mid, qi, f)
else:
Set(c*2+1, mid+1, se, qi, f)
mn[c] = min(mn[c*2], mn[c*2+1])
def Walk(c, ss, se, k):
if ss==se:
return ss+1 if mn[c]>k else ss
mid = ss+se>>1
if mn[c*2]>k:
return Walk(c*2+1, mid+1, se, k)
else:
return Walk(c*2, ss, mid, k)
las = [-1 for _ in range(n+2)]
was = [False for _ in range(n+3)]
for i in range(n):
if las[a[i]]!=i-1:
was[Walk(1, 1, n, las[a[i]])] = True
las[a[i]] = i
Set(1, 1, n, a[i], i)
for i in range(1, n+2):
if las[i]!=n-1:
was[Walk(1, 1, n, las[i])] = True
ans = 1
while was[ans]:
ans += 1
print(ans)
``` | output | 1 | 106,348 | 12 | 212,697 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6
Submitted Solution:
```
import sys, math
import io, os
data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
from itertools import permutations,combinations
#def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
#from decimal import Decimal
#from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9)+7
class SegmentTree:
def __init__(self, data, default=0, func=min):
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
while idx:
idx >>= 1
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
def __len__(self):
return self._len
def query(self, start, stop):
"""func of data[start, stop)"""
start += self._size
stop += self._size
res_left = res_right = self._default
while start < stop:
if start & 1:
res_left = self._func(res_left, self.data[start])
start += 1
if stop & 1:
stop -= 1
res_right = self._func(self.data[stop], res_right)
start >>= 1
stop >>= 1
return self._func(res_left, res_right)
def __repr__(self):
return "SegmentTree({0})".format(self.data)
n=int(data())
a=mdata()
if len(set(a))==1 and 1 in a:
out(1)
exit()
S=SegmentTree(list(range(-n-1,0)),INF)
m=[0]*(n+1)
for i in range(n):
x=a[i]
ind=S.query(0,x)
if ind<0:
m[ind]=1
else:
m[a[ind]-1]=1
S.__setitem__(x-1,i)
for i in range(n+1):
ind = S.query(0,i+1)
if ind < 0:
m[ind] = 1
else:
m[a[ind] - 1] = 1
for i in range(n+1):
if m[i]==0:
out(i+1)
exit()
out(n+2)
``` | instruction | 0 | 106,349 | 12 | 212,698 |
Yes | output | 1 | 106,349 | 12 | 212,699 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6
Submitted Solution:
```
import collections
n = int(input())
a = list(map(int, input().split()))
def lowbit(x):
return x & -x
def add(x, k):
i = x
while i <= n:
tree[i] = max(tree[i], k)
i += lowbit(i)
def query(x):
ans = 0
i = x
while i > 0:
ans = max(ans, tree[i])
i -= lowbit(i)
return ans
G = collections.defaultdict(list)
tree = collections.defaultdict(int)
for i in range(1, n + 2):
G[i].append(0)
for i in range(1, n + 1):
G[a[i - 1]].append(i)
for i in range(1, n + 2):
G[i].append(n + 1)
ans = 1
for i in range(1, n + 2):
flag, sz = 0, len(G[i])
if sz == n + 2:
ans = i
break
for j in range(1, sz):
f = query(G[i][j - 1] + 1)
if f < G[i][j]:
flag = 1
if not flag:
break
for j in range(1, sz):
f = G[i][j - 1] + 1
add(f, G[i][j])
ans = i + 1
print(ans)
``` | instruction | 0 | 106,350 | 12 | 212,700 |
Yes | output | 1 | 106,350 | 12 | 212,701 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6
Submitted Solution:
```
import collections
n = int(input())
a = list(map(int, input().split()))
def lowbit(x):
return x & -x
def add(x, k):
while x <= n:
tree[x] = max(tree[x], k)
x += lowbit(x)
def query(x):
ans = 0
while x > 0:
ans = max(ans, tree[x])
x -= lowbit(x)
return ans
G = collections.defaultdict(list)
tree = collections.defaultdict(int)
for i in range(1, n + 2):
G[i].append(0)
for i in range(1, n + 1):
G[a[i - 1]].append(i)
for i in range(1, n + 2):
G[i].append(n + 1)
ans = 1
for i in range(1, n + 2):
flag, sz = 0, len(G[i])
if sz == n + 2:
ans = i
break
for j in range(1, sz):
f = query(G[i][j - 1] + 1)
if f < G[i][j]:
flag = 1
if not flag:
break
for j in range(1, sz):
f = G[i][j - 1] + 1
add(f, G[i][j])
ans = i + 1
print(ans)
``` | instruction | 0 | 106,351 | 12 | 212,702 |
Yes | output | 1 | 106,351 | 12 | 212,703 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6
Submitted Solution:
```
import functools
n = int(input())
arr = [int(x) for x in input().split()]
functools.lru_cache(None)
def mex(a):
a.sort()
i = 1
while i in a:
i += 1
return i
grand_arr = []
for l in range(1, n+1):
for i in range(0, n-l+1):
subset = arr[i:i+l]
print(subset)
grand_arr.append(mex(subset))
print(mex(grand_arr))
``` | instruction | 0 | 106,352 | 12 | 212,704 |
No | output | 1 | 106,352 | 12 | 212,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6
Submitted Solution:
```
def mex(a, n):
p = [0 for i in range(n + 1)]
for i in range(n):
if i + 1 in a:
p[i] = 1
return p.index(0) + 1
n = int(input())
t = list(map(int, input().split()))
res = []
for i in range(1, n + 1):
for j in range(n - i + 1):
res.append(mex(set(t[j:j + i]), n))
print(mex(set(res), n))
``` | instruction | 0 | 106,353 | 12 | 212,706 |
No | output | 1 | 106,353 | 12 | 212,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6
Submitted Solution:
```
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def ii(): return int(input())
def si(): return input()
def mi(): return map(int,input().strip().split(" "))
def msi(): return map(str,input().strip().split(" "))
def li(): return list(mi())
def dmain():
sys.setrecursionlimit(1000000)
threading.stack_size(1024000)
thread = threading.Thread(target=main)
thread.start()
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def getKey(item): return item[1]
def sort2(l):return sorted(l, key=getKey,reverse=True)
def d2(n,m,num):return [[num for x in range(m)] for y in range(n)]
def isPowerOfTwo (x): return (x and (not(x & (x - 1))) )
def decimalToBinary(n): return bin(n).replace("0b","")
def ntl(n):return [int(i) for i in str(n)]
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def ceil(x,y):
if x%y==0:
return x//y
else:
return x//y+1
def powerMod(x,y,p):
res = 1
x %= p
while y > 0:
if y&1:
res = (res*x)%p
y = y>>1
x = (x*x)%p
return res
def gcd(x, y):
while y:
x, y = y, x % y
return x
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def main():
n,=I()
l=I()
now=[]
cur=1
for i in range(n):
if l[i]==cur:
now.append([i,i])
if not now:
print(1)
sys.exit()
def mex(l):
r=sorted(set(l))
for i in range(len(r)):
if i+1!=r[i]:
return i+1
return len(r)+1
cur=2
an=2
while 1:
nxt=[]
while now:
a,b=now.pop()
if a-1>=0 and l[a-1]==cur:
nxt.append([a-1,b])
if b+1<n and l[b+1]==cur:
nxt.append([a,b+1])
if not nxt:
an=cur+1
break
now=nxt
cur+=1
if cur>mex(l):
an-=1
print(an)
main()
``` | instruction | 0 | 106,354 | 12 | 212,708 |
No | output | 1 | 106,354 | 12 | 212,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.
The next line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the elements of the array.
Output
Print a single integer — the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
6
Submitted Solution:
```
ultimmex = []
def mex(x):
x = list(set(x))
x.sort()
if x[0]!=1:
return 1
else:
for i in range(1, len(x)+2):
if i not in x:
return i
break
n = int(input())
lst = list(map(int, input().split()))
temp = list(lst)
for i in range(1, len(temp)):
temp.pop(0)
ultimmex.append(mex(temp))
for i in range(1, len(lst)):
lst.pop()
ultimmex.append(mex(lst))
print(mex(ultimmex))
``` | instruction | 0 | 106,355 | 12 | 212,710 |
No | output | 1 | 106,355 | 12 | 212,711 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,356 | 12 | 212,712 |
Tags: dp, math, probabilities
Correct Solution:
```
from sys import stdin
t = int(stdin.readline())
for _ in range(t):
n, m = tuple(int(x) for x in stdin.readline().split())
data = tuple(int(x) for x in stdin.readline().split())
exp = []
for _ in range(m):
exp.append(stdin.readline().split())
for last in range(n-1, -1, -1):
if last != data[last] - 1:
break
else:
print(1.0)
continue
ans = 1
for e in exp:
if int(e[0]) > last:
ans *= (1-float(e[1]))
print(1-ans)
``` | output | 1 | 106,356 | 12 | 212,713 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,357 | 12 | 212,714 |
Tags: dp, math, probabilities
Correct Solution:
```
import sys
input=sys.stdin.readline
#print=sys.stdout.write
#sys.setrecursionlimit(100000)
#from heapq import *
#from collections import deque as dq
#from math import ceil,floor,sqrt,gcd,log
#import bisect as bs
#from collections import Counter
#from collections import defaultdict as dc
#from functools import reduce
#from functools import lru_cache
ri=lambda:int(input())
rl=lambda:list(map(int,input().split()))
rs=lambda:input().strip("\r\n")
for _ in range(ri()):
n,m=rl()
a=rl()
pos=n-1
while a[pos]==pos+1 and pos>=0:
pos-=1
ans=1
if pos==-1:
ans=0
for _ in range(m):
r,p=input().split()
r=int(r)-1
p=float(p)
if r>=pos:
ans=ans*(1-p)
print(1-ans)
``` | output | 1 | 106,357 | 12 | 212,715 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,358 | 12 | 212,716 |
Tags: dp, math, probabilities
Correct Solution:
```
t = int(input())
while t > 0:
t -= 1
n, m = map(int, input().split())
lst = list(map(int, input().split()))
lst.reverse()
k = n
for i in lst:
if i == k:
k -= 1
else:
break
ans = 1.0
for i in range(m):
r, p = map(float, input().split())
if r >= k:
ans *= (1.0 - p)
if k == 0:
print(1)
else:
print("{:.6f}".format(1.0 - ans))
``` | output | 1 | 106,358 | 12 | 212,717 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,359 | 12 | 212,718 |
Tags: dp, math, probabilities
Correct Solution:
```
for _ in range (int(input())) :
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
x = n
for i in range (n-1,-1,-1) :
if x != a[i] :
break
x -= 1
ans = 1
for _ in range (m) :
i,p = list(map(float, input().split()))
if int(i)>=x :
ans *= (1-p)
if (a == sorted(a)) :
print(1)
continue
print('%.15f'%(1-ans))
``` | output | 1 | 106,359 | 12 | 212,719 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,360 | 12 | 212,720 |
Tags: dp, math, probabilities
Correct Solution:
```
t = int(input())
for _ in range(t):
n,k = map(int,input().split())
arr = list(map(int,input().strip().split()))[:n]
p = 1;i = n-1
while i >=0:
if arr[i] != i+1:
break
i-=1
ans = 0
i+=1
for z in range(k):
a,b = map(float,input().split())
if a >= i:
ans += p*b
p = p*(1-b)
if i == 0:
ans = 1.0
print(ans)
``` | output | 1 | 106,360 | 12 | 212,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,361 | 12 | 212,722 |
Tags: dp, math, probabilities
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()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def print_list(l):
print(' '.join(map(str,l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
# import math
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
def find(data, mi, ma, s):
res.add(s)
a, b = [], []
p = (mi + ma) >> 1
for v in data:
if v <= p:
a.append(v)
else:
b.append(v)
if a and b:
sa = sum(a)
find(a, mi, max(a), sa)
find(b, min(b), ma, s - sa)
for _ in range(N()):
n, m = RL()
a = RLL()
key = 0
for i in range(n - 1, -1, -1):
if a[i] != i + 1:
key = i + 1
break
if key == 0:
for _ in range(m):
input()
print(1)
else:
s = 1
for _ in range(m):
o = input().split()
i = int(o[0])
p = float(o[1])
if i >= key:
s *= 1 - p
print(1 - s)
``` | output | 1 | 106,361 | 12 | 212,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,362 | 12 | 212,724 |
Tags: dp, math, probabilities
Correct Solution:
```
def proba(arr,po):
maxi=None
for i in range(len(arr)-1,-1,-1):
if arr[i]!=i+1:
maxi=i
break
if maxi==None:
return 1.0
ans=1
po=sorted(po,key=lambda s:s[0],reverse=True)
for i in range(len(po)):
if po[i][0]>=maxi:
ans*=(1-po[i][1])
else:
break
return 1-ans
for i in range(int(input())):
a,b=map(int,input().strip().split())
lst=list(map(int,input().strip().split()))
blanck=[]
for i in range(b):
x,y=map(str,input().strip().split())
x=int(x)-1
y=float(y)
blanck.append([x,y])
print(proba(lst,blanck))
``` | output | 1 | 106,362 | 12 | 212,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment. | instruction | 0 | 106,363 | 12 | 212,726 |
Tags: dp, math, probabilities
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n,m = map(int,input().split())
a = list(map(int,input().split()))
j = n-1
while j != -1 and a[j] == j+1:
j -= 1
if j == -1:
for _ in range(m):
x = input()
print(1)
continue
ans = 0
fac = 1
for _ in range(m):
r,p = map(float,input().split())
r = int(r)
if r >= j+1:
ans += fac*p
fac *= (1-p)
print(ans)
#Fast IO Region
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")
if __name__ == '__main__':
main()
``` | output | 1 | 106,363 | 12 | 212,727 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
for _ in range(int(input())):
n,m = list(map(int,input().split()))
arr = list(map(int,input().split()))
req = n
for i in range(n-1,-1,-1):
if arr[i]==i+1:
req = i
else:
break
ans = 0
p = 1
for i in range(m):
x,y = list(map(float,input().split()))
x = int(x)
if x>=req:
ans+=(p*y)
p = p*(1-y)
if req==0:
ans = 1
print(ans)
``` | instruction | 0 | 106,364 | 12 | 212,728 |
Yes | output | 1 | 106,364 | 12 | 212,729 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
t=int(input())
for asd in range(t):
n,k=map(int,input().split())
ar=list(map(int,input().split()))
br=[]
for i in range(1,n+1):
br.append(i)
idx=n-1
while idx>=0:
if ar[idx]!=br[idx]:
break
idx-=1
ans=1.0
if idx<=0:
ans=1.000000000
for i in range(k):
a,p=input().split()
else:
for i in range(k):
a,p = input().split()
a=int(a)
p=float(p)
a-=1
if a>=idx:
ans=ans*(1.0-p)
ans=1-ans
print("%.9f"%ans)
``` | instruction | 0 | 106,365 | 12 | 212,730 |
Yes | output | 1 | 106,365 | 12 | 212,731 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
import sys,math
from functools import reduce
N=int(input())
for i in range(N):
n,m=list(map(int,sys.stdin.readline().strip().split()))
arr=list(map(int,sys.stdin.readline().strip().split()))
lmt=n
for pos in range(n-1,-1,-1):
if arr[pos]==pos+1:
lmt-=1
else:
break
P=[]
for _ in range(m):
a,b=list(map(float,sys.stdin.readline().strip().split()))
if int(a)>=lmt:
P.append(b)
if lmt==0:
print(1.0)
elif len(P)==0:
print(0.0)
else:
# print(P)
print(1-reduce(lambda x,y:x*y,(map(lambda x:1-x,P))))
``` | instruction | 0 | 106,366 | 12 | 212,732 |
Yes | output | 1 | 106,366 | 12 | 212,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
for _ in range(int(input())):
n,m = map(int, input().split())
a = [*map(int, input().split())]
t = 1
for i in range(n-1,-1,-1):
if a[i]!=i+1:t = i+1;break
ans = 1
for i in range(m):
r, p = map(float, input().split())
if r>=t:
ans *= (1-p)
print(1-ans if t>1 else 1)
``` | instruction | 0 | 106,367 | 12 | 212,734 |
Yes | output | 1 | 106,367 | 12 | 212,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
import sys, os
# import numpy as np
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
from itertools import permutations
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))
# read_f = lambda file: list(map(int, file.readline().strip().split()))
# from time import time
# sys.setrecursionlimit(5*10**6)
MOD = 10**9 + 7
def f(row, a, b, curr, mat, pref):
if row >= 0 and row < len(mat) and a >= 0 and b < len(mat[0]):
# print(a, b, pref[row][a], pref[row][b], curr)
if a == 0:
return(pref[row][b] == curr)
else:
return((pref[row][b] - pref[row][a-1]) == curr)
else:
return(False)
def main():
# file1 = open("C:\\Users\\shank\\Desktop\\Comp_Code\\input.txt", "r")
# n = int(file1.readline().strip());
# arr = list(map(int, file1.read().strip().split(" ")))
# file1.close()
# n = int(input())
ans = []
for _ in range(int(input())):
n, m = read(); arr = [0]+read()
till = n+1
for i in range(n, 0, -1):
if i != arr[i]:
till = i+1
break
p = [0]*(n+1)
for i in range(m):
a, prob = input().strip().split()
a = int(a); prob = float(prob)
p[a] = prob
s = 1
for i in range(n, 0, -1):
if i < till-1:
s = s*p[i] + s*(1.00000000-p[i])
else:
s = s*(1.0000000-p[i])
if arr == sorted(arr):
print(1.000000000)
# ans.append(1.0000000)
else:
print(1.0000000 - s)
# ans.append(round((1.0000000 - s), 6))
# print(("\n").join(map(str, ans)))
# print(("\n").join(map(str, ans)))
# file = open("output.txt", "w")
# file.write(ans+"\n")
# file.close()
if __name__ == "__main__":
main()
"""
abcabcabc
"""
``` | instruction | 0 | 106,368 | 12 | 212,736 |
No | output | 1 | 106,368 | 12 | 212,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
for w in range(int(input())):
n,m=tuple(map(int,input().split()))
a=list(map(int,input().split()))
d={}
x=-1
for i in reversed(range(n)):
if(a[i]!=i+1):
x=i+1
break
for i in range(m):
l1=input().split()
r=int(l1[0])
p=float(l1[1])
if(r>=x):
d[r]=p
d1=sorted(list(d.keys()))
ans=0
y=1
i=len(d1)-1
while(i>=0):
ans+=y*d[d1[i]]
y*=(1-d[d1[i]])
i-=1
if(d1[0]>x):
ans+=y
print(ans)
#print(d,x)
``` | instruction | 0 | 106,369 | 12 | 212,738 |
No | output | 1 | 106,369 | 12 | 212,739 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
from sys import stdin
t=int(stdin.readline())
for _ in range(t):
n,k=map(int,stdin.readline().split())
arr=list(map(int,stdin.readline().split()))
srr=sorted(arr)
prob=0
prev=0
di={}
for i in range(k):
a,b=map(float,stdin.readline().split())
di[int(a)]=b
counter=0
for i in range(n):
if arr[i]==srr[i]:
if srr[i] in di:
prob=prob*(1-di[srr[i]])+di[srr[i]]
else:
counter=1
if srr[i] in di:
prob=di[srr[i]]
else:
prob=0
if counter==0:
print(1)
else:
print(round(prob,6))
``` | instruction | 0 | 106,370 | 12 | 212,740 |
No | output | 1 | 106,370 | 12 | 212,741 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 100).
The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 10^5) — the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 ≤ r_i ≤ n, 0 ≤ p_i ≤ 1) — the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (∑ n, ∑ m ≤ 10^5).
Output
For each test case, print a single number — the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = list(map(int, input().split()))
lc = n-1
for i in range(n-1, 0, -1):
if (a[i] == i+1):
lc = i
else:
break
if (lc == 0):
print(1)
break
ans = 1.0
for _ in range(m):
data = input().split()
r = int(data[0])
p = float(data[1])
if (r >= lc):
ans *= 1-p
print("{:.6f}".format(1-ans))
``` | instruction | 0 | 106,371 | 12 | 212,742 |
No | output | 1 | 106,371 | 12 | 212,743 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,372 | 12 | 212,744 |
Tags: binary search, data structures, dp
Correct Solution:
```
def find(x):
pre=[0]*(n+1)
cur=0
for i in range(n):
if(a[i]<x):
cur-=1
else:
cur+=1
if(i>=k-1):
if(cur-pre[i-k]>0):
return 1
pre[i]=min(pre[i-1],cur)
return 0
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=1
r=n
while(l<=r):
m=(l+r)//2
if(find(m)):
l=m+1
else:
r=m-1
print(r)
``` | output | 1 | 106,372 | 12 | 212,745 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,373 | 12 | 212,746 |
Tags: binary search, data structures, dp
Correct Solution:
```
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
l = 1
r = n + 1
while r - l > 1:
mid = (r + l) // 2
b = [1 if i >= mid else -1 for i in a]
for i in range(1, len(b)):
b[i] += b[i - 1]
ans = False
if b[k - 1] > 0:
ans = True
mn = 0
for i in range(k, len(b)):
mn = min(mn, b[i - k])
if b[i] - mn > 0:
ans = True
if ans:
l = mid
else:
r = mid
print(l)
``` | output | 1 | 106,373 | 12 | 212,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,374 | 12 | 212,748 |
Tags: binary search, data structures, dp
Correct Solution:
```
from sys import stdin
from collections import Counter
from math import pow, sqrt, factorial, log10, log
from itertools import permutations, combinations, combinations_with_replacement
input = stdin.readline
def li(): return list(map(int, input().split()))
def mp(): return map(int, input().split())
def inp(): return int(input())
def pr(n): return stdout.write(str(n)+"\n")
INF = float('inf')
def solve():
n, k = mp()
a = li()
l, r = 1, n
while l < r:
m = (l+r+1) >> 1
b = [0] * (n+1)
for i in range(n):
if a[i] >= m:
b[i+1]=1
else:
b[i+1]=-1
b[i+1]+=b[i]
x, f = 0, 0
for i in range(k, n+1):
x = min(x, b[i-k])
if x<b[i]:
f=1
break
if f:
l=m
else:
r = min(r-1, m)
print(l)
t = 1
for i in range(t):
solve()
``` | output | 1 | 106,374 | 12 | 212,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,375 | 12 | 212,750 |
Tags: binary search, data structures, dp
Correct Solution:
```
import sys
input = sys.stdin.readline
n,k=map(int,input().split())
A=list(map(int,input().split()))
MIN=min(A)
MAX=max(A)
OK=MIN
NG=MAX+1
seg_el=1<<((n+1).bit_length())
def update(n,x,seg_el):
i=n+seg_el
SEG[i]=x
i>>=1
while i!=0:
SEG[i]=max(SEG[i*2],SEG[i*2+1])
i>>=1
def getvalues(l,r):
L=l+seg_el
R=r+seg_el
ANS=-1<<30
while L<R:
if L & 1:
ANS=max(ANS , SEG[L])
L+=1
if R & 1:
R-=1
ANS=max(ANS , SEG[R])
L>>=1
R>>=1
return ANS
while NG-OK>1:
mid=(OK+NG)//2
S=[0]
for a in A:
if a>=mid:
S.append(S[-1]+1)
else:
S.append(S[-1]-1)
SEG=[-1<<30]*(2*seg_el)
for i in range(n+1):
SEG[i+seg_el]=S[i]
for i in range(seg_el-1,0,-1):
SEG[i]=max(SEG[i*2],SEG[i*2+1])
#print(mid,SEG)
for i in range(n+1-k):
t=S[i]
if getvalues(i+k,n+2)>t:
OK=mid
#print(mid,i,i+k,n+2)
break
else:
NG=mid
print(OK)
``` | output | 1 | 106,375 | 12 | 212,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,376 | 12 | 212,752 |
Tags: binary search, data structures, dp
Correct Solution:
```
def proc1(m: int, arr: list) -> None:
global cnt1
global cnt2
cnt1 = [0] * (n + 2)
cnt2 = [0] * (n + 2)
for i in range(1, n + 1):
if arr[i] < m:
cnt1[i] = cnt1[i-1] - 1
else:
cnt1[i] = cnt1[i-1] + 1
for i in range(n, 0, -1):
if arr[i] < m:
cnt2[i] = max(0, cnt2[i+1] - 1)
else:
cnt2[i] = cnt2[i+1] + 1
pass
def proc2(m: int, k: int, arr: list) -> bool:
global cnt1
global cnt2
proc1(m, arr)
for i in range(1, n + 1):
if i + k > n + 1:
break
c1 = cnt1[i + k - 1] - cnt1[i - 1]
c2 = cnt2[i + k]
if c1 + c2 > 0:
return True
return False
def solve(n: int, k: int, arr: list) -> int:
arr2 = arr[1:n+1]
arr2.sort()
l, r = 1, n
while l + 1 < r:
mid = (l + r) // 2
median = arr2[mid - 1]
if proc2(median, k, arr):
l = mid
else:
r = mid - 1
if proc2(arr2[r - 1], k, arr):
return arr2[r - 1]
else:
return arr2[l - 1]
pass
if __name__ == '__main__':
read = input().split()
n = int(read[0])
k = int(read[1])
read = input().split()
arr = [0] * (n + 2)
for i in range(1, n + 1):
arr[i] = int(read[i - 1])
ans = solve(n, k, arr)
print(ans)
pass
``` | output | 1 | 106,376 | 12 | 212,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,377 | 12 | 212,754 |
Tags: binary search, data structures, dp
Correct Solution:
```
N,K=map(int,input().split())
A=list(map(int,input().split()))
L,R=1,N
while L<R:
M=(L+R+1)>>1
B=[0]*(N+1)
for i in range(N):
if A[i]>=M:
B[i+1]=1
else:
B[i+1]=-1
B[i+1]+=B[i]
X=0
F=0
for i in range(K,N+1):
X=min(X,B[i-K])
if X<B[i]:
F=1
break
if F:
L=M
else:
R=min(R-1,M)
print(L)
``` | output | 1 | 106,377 | 12 | 212,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,378 | 12 | 212,756 |
Tags: binary search, data structures, dp
Correct Solution:
```
#######################################################################################################################
# Author: BlackFyre
# Language: PyPy 3.7
#######################################################################################################################
from sys import stdin, stdout, setrecursionlimit
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log, log2
from random import seed, randint
from datetime import datetime
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
from collections import defaultdict as dd
mod = pow(10, 9) + 7
mod2 = 998244353
# setrecursionlimit(3000)
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var) + "\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def smp(): return map(str, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def remadd(x, y): return 1 if x % y else 0
def ceil(a, b): return (a + b - 1) // b
def def_value(): return 0
n,k = mp()
a = lmp()
b = sorted(a)
l,r = 1,n
while l<r:
mid = (l+r+1)//2
x = mid
tmp = a.copy()
for i in range(n):
if tmp[i]>=x: tmp[i]=1
else: tmp[i]=-1
sm = l1d(n+1)
for i in range(n):
sm[i+1]=sm[i]+tmp[i]
flg=False
mn = 0
for i in range(-1,n-k):
mn = min(mn,sm[i+1])
if sm[i+k+1]-mn>0:
flg=True
break
if flg: l=mid
else: r=mid-1
print(l)
``` | output | 1 | 106,378 | 12 | 212,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3. | instruction | 0 | 106,379 | 12 | 212,758 |
Tags: binary search, data structures, dp
Correct Solution:
```
import sys, math, itertools, random, bisect
from collections import defaultdict
INF = sys.maxsize
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def input(): return sys.stdin.readline().strip()
mod = 10**9 + 7
def check(a,m,k):
b = [1 if i>=m else -1 for i in a]
for i in range(1,len(b)):
b[i] += b[i-1]
ans = False
if b[k-1]>0: ans = True
mn = 0
for i in range(k,len(b)):
mn = min(mn,b[i-k])
if b[i]-mn>0: ans = True
return ans
for _ in range(1):
n,k = get_ints()
a = get_array()
l,r = 1,n
while l<=r:
m = (l+r)//2
if check(a,m,k): l = m+1
else: r = m-1
print(r)
``` | output | 1 | 106,379 | 12 | 212,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def check(n,k,a,mid):
a = list(map(lambda xx:xx>=mid,a))
cul = sum(a[:k])
if cul >= k//2+1:
return 1
mino,mine,pr = 0,10**10,0
for i in range(n-k):
pr += a[i]
cul += a[i+k]
if i&1:
mino = min(mino,pr-(i+1)//2)
else:
mine = min(mine,pr-i//2)
if (((k+i)&1 and cul-min(mino,mine-1) >= (k+i+1)//2+1) or
(not (k+i)&1 and cul-min(mino,mine) >= (k+i+1)//2+1)):
return 1
return 0
def main():
n,k = map(int,input().split())
a = list(map(int,input().split()))
hi,lo,ans = n,1,1
while hi >= lo:
mid = (hi+lo)//2
if check(n,k,a,mid):
lo = mid+1
ans = mid
else:
hi = mid-1
print(ans)
# Fast IO Region
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")
if __name__ == "__main__":
main()
``` | instruction | 0 | 106,380 | 12 | 212,760 |
Yes | output | 1 | 106,380 | 12 | 212,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
p = [0] * n
mx = [0] * n
def check(x):
for i in range(n):
cur = 1 if a[i] >= x else -1
if i == 0:
mx[i] = p[i] = cur
else:
p[i] = p[i - 1] + cur
mx[i] = max(cur, mx[i - 1] + cur)
for i in range(k - 1, n):
if p[i] - p[i - k + 1] + mx[i - k + 1] > 0:
return 1
return 0
ans = 0
for j in range(20, -1, -1):
ans += 1 << j
if not check(ans):
ans -= 1 << j
print(ans)
``` | instruction | 0 | 106,381 | 12 | 212,762 |
Yes | output | 1 | 106,381 | 12 | 212,763 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
def check(A, x, k):
n = len(A)
C = [0]
for i in range(len(A)):
if A[i] >= x:
C.append(C[-1] + 1)
else:
C.append(C[-1] - 1)
mn = C[0]
for i in range(k, n + 1):
mn = min(mn, C[i - k])
if C[i] - mn > 0:
return True
return False
n, k = map(int, input().split())
begin = 1
A = list(map(int, input().split()))
end = max(A)
while end - begin >= 2:
middle = (end + begin) // 2
if check(A, middle, k):
begin = middle
else:
end = middle
if check(A, end, k):
print(end)
else:
print(begin)
``` | instruction | 0 | 106,382 | 12 | 212,764 |
Yes | output | 1 | 106,382 | 12 | 212,765 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
n,k=map(int,input().split())
l=list(map(int,input().split()))
lo=1
hi=n
ans=1
z=0
while lo<=hi:
m=(lo+hi)//2
l2=[]
l1=[0]*n
for i in range(n):
if l[i]<m:
l2.append(-1)
else:
l2.append(1)
for i in range(1,n):
l2[i]+=l2[i-1]
c=-n
for i in range(n-1,-1,-1):
c=max(c,l2[i])
l1[i]=c
c=n
f=0
if l1[k-1]>0:
f=1
c=l2[0]
for i in range(1,n-k+1):
a=l1[i+k-1]
if a-c>0:
f=1
break
c=min(c,l2[i])
if f:
ans=m
lo=m+1
else:
hi=m
if lo==hi:
if z:
break
z+=1
print(ans)
``` | instruction | 0 | 106,383 | 12 | 212,766 |
Yes | output | 1 | 106,383 | 12 | 212,767 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
import math
import statistics
n, k = map(int, input().split(' '))
v = list(map(int, input().split(' ')))
ans = 0
for i in range(0, n-k+1):
j = k - 1
while i + j < n:
ans = max(ans, statistics.median(v[i:i+j]))
j += 1
print(math.floor(ans))
``` | instruction | 0 | 106,384 | 12 | 212,768 |
No | output | 1 | 106,384 | 12 | 212,769 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
import bisect
n, k = map(int,input().split())
arr = list(map(int,input().split()))
n = 200000
k = 100000
arr = range(200000)
temp = []
for i in range(k):
bisect.insort(temp,arr[i])
ans = temp[(k-1)//2]
for i in range(k,n):
loc = bisect.bisect_left(temp,arr[i-k])
temp.pop(loc)
bisect.insort(temp,arr[i])
ans = max(ans, temp[(k-1)//2])
print(ans)
``` | instruction | 0 | 106,385 | 12 | 212,770 |
No | output | 1 | 106,385 | 12 | 212,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
n,k=[int(x) for x in input().split()]
arr=[int(x) for x in input().split()]
arr.sort()
ans1=arr[n-k:]
ans2=arr[n-k-1:]
a=ans1[(len(ans1)+1)//2-1]
b=ans2[(len(ans2)+1)//2-1]
print(min(a,b))
``` | instruction | 0 | 106,386 | 12 | 212,772 |
No | output | 1 | 106,386 | 12 | 212,773 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number ⌊ (n + 1)/(2) ⌋ after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},…,a_r for some 1 ≤ l ≤ r ≤ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).
Output
Output one integer m — the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..4]) = 3.
Submitted Solution:
```
from sys import stdin
from collections import Counter
from math import pow, sqrt, factorial, log10, log
from itertools import permutations, combinations, combinations_with_replacement
input = stdin.readline
def li(): return list(map(int, input().split()))
def mp(): return map(int, input().split())
def inp(): return int(input())
def pr(n): return stdout.write(str(n)+"\n")
INF = float('inf')
def solve():
n, k = mp()
a = li()
b = list(a)
b.sort()
if k == 2:
return b[-2]
res = 0
origin = 0
while (origin <= n-k):
i = origin
j = origin + k
while(j <= n):
t = a[i:j]
t.sort()
l = len(t)
m = (l+1)//2
res = max(res, m)
i+=1
j+=1
origin+=1
return res
t = 1
for i in range(t):
print(solve())
``` | instruction | 0 | 106,387 | 12 | 212,774 |
No | output | 1 | 106,387 | 12 | 212,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 106,524 | 12 | 213,048 |
Tags: dp, implementation, two pointers
Correct Solution:
```
#ff = open('input.txt')
#for i in range(8):
n = int(input())
#n = int(ff.readline())
a = [int(x) for x in input().split()]
#a = [int(x) for x in ff.readline().split()]
vcnt = 1
vpl = [0]
vpr = []
for i in range(1, n):
if (a[i - 1] >= a[i]):
vcnt += 1
vpr.append(i - 1)
vpl.append(i)
if len(vpl) > len(vpr):
vpr.append(n - 1)
mmax = 0
for i in range(vcnt):
if vpr[i] - vpl[i] + 1 > mmax:
mmax = vpr[i] - vpl[i] + 1
if mmax < len(a):
mmax += 1
mmax1 = 0;
for i in range(vcnt - 1):
if (vpr[i] - vpl[i] > 0) and (vpr[i + 1] - vpl[i + 1] > 0):
if (a[vpr[i] - 1] + 1 < a[vpl[i + 1]]) or (a[vpr[i]] < a[vpl[i + 1] + 1] - 1):
mm = vpr[i] - vpl[i] + vpr[i + 1] - vpl[i + 1] + 2
if mm > mmax1:
mmax1 = mm
if mmax1 > mmax:
print(mmax1)
else:
print(mmax)
``` | output | 1 | 106,524 | 12 | 213,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 106,525 | 12 | 213,050 |
Tags: dp, implementation, two pointers
Correct Solution:
```
elems = int(input())
data = list(map(int, input().split()))
data.append(float("-inf"))
def find_seq_breaks():
global elems
global data
seq_spans = []
sequence_idx = 0
longest_span = 0
for i in range(1, elems + 1):
if data[i] <= data[i - 1]:
seq_range = [sequence_idx, i - 1]
seq_spans.append(seq_range)
diff = i - sequence_idx
if (longest_span < diff):
longest_span = diff
sequence_idx = i
return (seq_spans, longest_span)
def parse_seqs(seq_spans, longest_span):
global data
global elems
if len(seq_spans) > 1:
longest_span += 1
for i in range(1, len(seq_spans)):
one_past = seq_spans[i][0]
if one_past >= 2 or one_past <= elems - 1:
#look at values before and after num that broke seq
r = data[one_past] - data[one_past-2]
r2 = data[one_past + 1] - data[one_past - 1]
candidate = seq_spans[i][1] - seq_spans[i - 1][0] + 1
if r >= r2:
if r >= 2:
if longest_span < candidate:
longest_span = candidate
elif r2 >= 2:
if r2 >= 2:
if longest_span < candidate:
longest_span = candidate
return longest_span
d = find_seq_breaks()
print(parse_seqs(d[0], d[1]))
``` | output | 1 | 106,525 | 12 | 213,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 106,526 | 12 | 213,052 |
Tags: dp, implementation, two pointers
Correct Solution:
```
def canMerge(seq1,seq2):
if len(seq2) == 1:
return True
last = seq1[-1]
second = seq2[1]
if second-last > 1:
return True
if len(seq1) == 1:
return True
second_last = seq1[-2]
first = seq2[0]
if first-second_last > 1:
return True
return False
def main():
n = int(input())
arr = list(map(int,input().split()))
sequences = []
seq = [arr[0]]
for i in range(1,n):
if arr[i] > seq[-1]:
seq.append(arr[i])
else:
sequences.append(seq)
seq = [arr[i]]
sequences.append(seq)
ans = len(sequences[-1])
for i in range(len(sequences)-1):
seq1 = sequences[i]
seq2 = sequences[i+1]
ans = max(ans,len(seq1)+1,len(seq2)+1)
if canMerge(seq1,seq2):
ans = max(ans,len(seq1)+len(seq2))
print(ans)
main()
``` | output | 1 | 106,526 | 12 | 213,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 106,527 | 12 | 213,054 |
Tags: dp, implementation, two pointers
Correct Solution:
```
n = int(input())
sequence = [float('-inf')] + [int(i) for i in input().split()] + [float('inf')]
f1 = [0] * n
f2 = [0] * n
f3 = [True if sequence[i + 1] - sequence[i - 1] >
1 else False for i in range(1, len(sequence) - 1)]
len_1 = 0
len_2 = 0
l = len(sequence) - 1
for i in range(1, len(sequence) - 1):
f1[i-1] = len_1
f2[i-1] = len_2
if sequence[i] > sequence[i - 1]:
len_1 += 1
else:
len_1 = 1
if sequence[l - i] < sequence[l - i + 1]:
len_2 += 1
else:
len_2 = 1
f2 = f2[::-1]
f = [f1[i] + f2[i] + 1 if f3[i]
else max(f1[i], f2[i]) + 1 for i in range(0, n)]
max_len = max(f)
print(max_len)
'''
n = int(input())
sequence = [float('-inf')] + [int(i) for i in input().split()] + [float('inf')]
len_1 = 1
len_2 = 0
max_len = 1
i = 1
while(i < n):
if sequence[i] < sequence[i + 1]:
len_1 += 1
i += 1
else:
l = len_1 + len_2 + 1
if l > max_len:
max_len = l
len_1, len_2 = 1, len_1
if sequence[i + 2] - sequence[i] <= 1:
if sequence[i + 1] - sequence[i - 1] > 1:
len_2 -= 1
else:
len_2 = 0
i += 1
else:
i += 2
if max_len < len_1 + len_2 + 1:
max_len = len_1 + len_2 + 1
if max_len > n:
max_len = n
print(max_len)
'''
``` | output | 1 | 106,527 | 12 | 213,055 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Examples
Input
6
7 2 3 1 5 6
Output
5
Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4. | instruction | 0 | 106,528 | 12 | 213,056 |
Tags: dp, implementation, two pointers
Correct Solution:
```
n = int(input())
a = [0] + list(map(int, input().split()))
start = [0] * (n + 1)
end = [0] * (n + 1)
s, e = 1, 1
for i in range(2, n + 1):
e += 1
if a[i] <= a[i - 1]:
start[s:e] = [s] * (e-s)
end[s:e] = [e - 1] * (e-s)
s = e
start[s:] = [s] * (n-s+1)
end[s:] = [n] * (n-s+1)
best = 1
for i in range(2, n):
if a[i - 1] + 1 < a[i + 1]:
best = max(best, end[i + 1] - start[i - 1] + 1)
else:
best = max(best, i - start[i - 1] + 1, end[i + 1] - i + 1)
if n != 1:
best = max(best, n - start[n - 1] + 1, end[2])
print(best)
``` | output | 1 | 106,528 | 12 | 213,057 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.