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.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd. | instruction | 0 | 49,835 | 12 | 99,670 |
Tags: brute force, implementation, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n,x = map(int,input().split())
l = list(map(int,input().split()))
even = 0
odd = 0
for num in l:
if num%2 == 0:
even += 1
else:
odd += 1
temp = 1
flag = False
while temp<=odd and temp <= x:
need = x - temp
if even>= need:
flag = True
break
temp += 2
if flag:
print("Yes")
else:
print("No")
``` | output | 1 | 49,835 | 12 | 99,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd. | instruction | 0 | 49,836 | 12 | 99,672 |
Tags: brute force, implementation, math
Correct Solution:
```
# 1 + 2 = 3
# 2 + 2 = 4
# 5 + 5 = 10
#
#
for i in range(int(input())):
nechot = []
chot = []
n,x = map(int,input().split())
massive = list(map(int,input().split()))
for j in massive:
if j % 2 !=0:
nechot.append(j)
else:
chot.append(j)
if len(nechot) == 0 or (len(nechot)%2 ==0 and x == n) or (x%2 == 0 and len(nechot) == n):
print("No")
else:
print("Yes")
``` | output | 1 | 49,836 | 12 | 99,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd. | instruction | 0 | 49,837 | 12 | 99,674 |
Tags: brute force, implementation, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
odd = 0
even = 0
for i in range(n):
if a[i] % 2 == 0:
even += 1
else: odd += 1
if odd == 0:
print("No")
else:
if even == 0:
if x % 2 == 0:
print("No")
else:
print("Yes")
else:
if odd % 2 != 0:
print("Yes")
else:
if odd - 1 + even >= x:
print("Yes")
else:
print("No")
``` | output | 1 | 49,837 | 12 | 99,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd. | instruction | 0 | 49,838 | 12 | 99,676 |
Tags: brute force, implementation, math
Correct Solution:
```
for _ in range(int(input())):
n, x = map(int, input().split())
a = [*map(int, input().split())]
evens= sum(1 for i in a if i%2==0)
odds = n - evens
ans = 'NO'
for i in range(x):
if i <= evens and x-i <= odds and (x-i)%2 == 1:
ans = 'YES'
break
print(ans)
``` | output | 1 | 49,838 | 12 | 99,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd. | instruction | 0 | 49,839 | 12 | 99,678 |
Tags: brute force, implementation, math
Correct Solution:
```
import os
### START FAST IO ###
os_input = os.read(0, int(1e7)).split()
os_input_pos = -1
answer_list = []
def read_s():
global os_input_pos
os_input_pos += 1
return os_input[os_input_pos].decode()
def read_i():
return int(read_s())
def write_s(v):
answer_list.append(v)
def write_i(v):
write_s(str(v))
def print_ans():
os.write(1, "\n".join(answer_list).encode())
os.write(1, "\n".encode())
#### END FAST IO ####
T = read_i()
while T:
T -= 1
n = read_i()
x = read_i()
a = [read_i() % 2 for i in range(n)]
if x % 2:
write_s("Yes" if a.count(1) and (x < n or a.count(1) % 2) else "No")
else:
write_s("Yes" if a.count(1) and a.count(0) and (x < n or a.count(1) % 2) else "No")
print_ans()
``` | output | 1 | 49,839 | 12 | 99,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd. | instruction | 0 | 49,840 | 12 | 99,680 |
Tags: brute force, implementation, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n,x = map(int,input().split())
arr = list(map(int,input().split()))
odds = len([i for i in arr if i%2!=0])
evens = len(arr) - odds
yes = False
for i in range(1,odds+1,2):
if i>x:
break
if x-i<=evens:
yes = True
break
if yes:
print("YES")
else:
print("NO")
``` | output | 1 | 49,840 | 12 | 99,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd. | instruction | 0 | 49,841 | 12 | 99,682 |
Tags: brute force, implementation, math
Correct Solution:
```
from sys import stdin, stdout
def find2(arr,N,X):
a,b=0,0
for i in arr:
if(i%2): a+=1
else: b+=1
while((X%2==0 or X>a) and b>0):
X-=1
b-=1
if X%2==0 or X>a: return 'No'
return 'Yes'
def find(arr,N,X):
a,b=0,0
for i in arr:
if(i%2): a+=1
else: b+=1
if a<1: return 'No'
a-=1; X-=1
X-=min(X//2, a//2)*2
if X<=b: return 'Yes'
return 'No'
def main():
for _ in range(int(stdin.readline())):
N,X=list(map(int, stdin.readline().split()))
arr=list(map(int, stdin.readline().split()))
print(find(arr,N,X))
main()
``` | output | 1 | 49,841 | 12 | 99,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
for _ in range(int(input())):
n,x=map(int,input().split())
a=list(map(int,input().split()))
od=0
ev=0
for i in range(n):
if(a[i]%2==0):
ev+=1
else:
od+=1
if(ev!=0 and od!=0):
if(x==n):
if(od%2!=0):
print("Yes")
else:
print("No")
else:
print("Yes")
else:
if(x%2==1 and od!=0):
print("Yes")
else:
print("No")
``` | instruction | 0 | 49,842 | 12 | 99,684 |
Yes | output | 1 | 49,842 | 12 | 99,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
rr = lambda: input().strip()
rrm = lambda: map(int, rr().split())
def solve(n,x,a):
o,e = 0,0
for i in range(n):
if(a[i]%2):
o += 1
else:
e += 1
if(n==x):
if(o%2):
return "Yes"
else:
return "No"
if(o==0):
return "No"
elif(o>=x):
if(x%2):
return "Yes"
elif(e==0):
return "No"
else:
return "Yes"
elif(o<x):
return "Yes"
T = int(rr())
for _ in range(T):
n, x = rrm()
a = list(rrm())
ans = solve(n,x,a)
print(ans)
``` | instruction | 0 | 49,843 | 12 | 99,686 |
Yes | output | 1 | 49,843 | 12 | 99,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
for _ in range(int(input())):
n,x=map(int,input().split())
a=list(map(int,input().split()))
odd=0
even=0
for i in a:
if i%2==1:
odd+=1
else:
even+=1
if odd==0:
print('NO')
continue
if even==0:
print('YES' if x%2==1 else 'NO')
continue
iter=1
while(iter<=odd):
if iter+2>odd:
break
iter+=2
if x-iter<=even:
print('YES')
else:
print('NO')
``` | instruction | 0 | 49,844 | 12 | 99,688 |
Yes | output | 1 | 49,844 | 12 | 99,689 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
t=int(input())
while t:
t=t-1
n,x=map(int,input().split())
a=list(map(int,input().split()))
flag=1
oc=0
ec=0
for i in a:
if i%2==0:
ec+=1
else:
oc+=1
if oc>0:
if oc%2==0:
oc-=1
if x%2:
x-=min(x,oc)
if ec<x:
flag=0
else:
x-=min(x-1,oc)
if ec<x:
flag=0
else:
flag=0
if flag==1:
print('Yes')
else:
print('No')
``` | instruction | 0 | 49,845 | 12 | 99,690 |
Yes | output | 1 | 49,845 | 12 | 99,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
def task_A():
n = int(input())
for _ in range(n):
len_array, exp_count = map(int, input().split())
even_count = 0
odd_count = 0
numbers = [int(n) for n in input().split()]
for i in range(len_array):
curr = numbers[i]
if curr % 2:
odd_count += 1
else:
even_count += 1
#print(odd_count, even_count)
if odd_count == 0:
print('No')
elif exp_count % 2 == 0:
if (even_count % 2 == 1):
print('Yes')
else:
print('No')
else:
if (even_count % 2 == 0):
print('Yes')
else:
print('No')
def main():
task_A()
if __name__ == '__main__':
main()
``` | instruction | 0 | 49,846 | 12 | 99,692 |
No | output | 1 | 49,846 | 12 | 99,693 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
for i in range(int(input())):
n,x=map(int,input().split())
a=list(map(int,input().split()))
q,w,t=0,0,1
for i in a:
if i&1:q+=1
else:w+=1
if q==0:t=0
if q%2==0:x-=q-1
else:x-=q
if x>w:t=0
print("Yes" if t else"No")
``` | instruction | 0 | 49,847 | 12 | 99,694 |
No | output | 1 | 49,847 | 12 | 99,695 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
for _ in range(int(input())):
n,x=map(int,input().split())
ar=list(map(int,input().split()))
o,e=0,0
for i in ar:
if i%2: o+=1
else : e+=1
if x%2==0:
if o==0 or e==0: print('No')
else: print('Yes')
else:
if o>=x: print('Yes')
else:
if o%2: print('Yes')
elif e>=x-o+1: print('Yes')
else: print('No')
``` | instruction | 0 | 49,848 | 12 | 99,696 |
No | output | 1 | 49,848 | 12 | 99,697 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Shubham has an array a of size n, and wants to select exactly x elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.
Tell him whether he can do so.
Input
The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and x (1 ≤ x ≤ n ≤ 1000) — the length of the array and the number of elements you need to choose.
The next line of each test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) — elements of the array.
Output
For each test case, print "Yes" or "No" depending on whether it is possible to choose x elements such that their sum is odd.
You may print every letter in any case you want.
Example
Input
5
1 1
999
1 1
1000
2 1
51 50
2 2
51 50
3 3
101 102 103
Output
Yes
No
Yes
Yes
No
Note
For 1st case: We must select element 999, and the sum is odd.
For 2nd case: We must select element 1000, so overall sum is not odd.
For 3rd case: We can select element 51.
For 4th case: We must select both elements 50 and 51 — so overall sum is odd.
For 5th case: We must select all elements — but overall sum is not odd.
Submitted Solution:
```
test = int(input())
for tests in range(test):
arr = [int(i) for i in input().split()]
n = arr[0]
k = arr[1]
arr = [int(i) for i in input().split()]
orr = []
err = []
for arrs in arr:
if arrs % 2 == 0:
err.append(0)
else:
orr.append(1)
even = len(err)
odd = len(orr)
# print(k-even)
# print(odd,even)
if odd % 2 == 1:
print("Yes")
else:
if odd == 0:
print("No")
elif k == n:
print("No")
elif even == 0 and k%2==0:
print("no")
else:
print("Yes")
``` | instruction | 0 | 49,849 | 12 | 99,698 |
No | output | 1 | 49,849 | 12 | 99,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,850 | 12 | 99,700 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
def lower_bound(self, w):
#if w <= 0:
# return 0
x = 0
k = 1 << (self.size.bit_length() - 1)
while k:
if x + k <= self.size and self.tree[x + k] > w:
w -= self.tree[x + k]
x += k
k >>= 1
return x + 1
N, Q = map(int, input().split())
A = list(map(int, input().split()))
A = [i+1 - A[i] for i in range(N)]
query = [[] for _ in range(N+1)]
ans = [0] * Q
for q in range(Q):
x, y = map(int, input().split())
l = x+1
r = N-y
query[r].append((l, q))
bit = Bit(N+1)
for r in range(1, N+1):
a = A[r-1]
if a == 0:
bit.add(1, +1)
bit.add(r+1, -1)
elif a > 0:
#if the value of l is greater than k, it means that we cannot
#remove the particular number from the array, as there are not
#sufficient removals before it, if the segment starts from l
k = bit.lower_bound(a-1)
#print(k,r,a)
if k <= r:
bit.add(1, +1)
bit.add(k, -1)
for l, q in query[r]:
ans[q] = bit.sum(l)
print(*ans, sep="\n")
if __name__ == '__main__':
main()
``` | output | 1 | 49,850 | 12 | 99,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,851 | 12 | 99,702 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
n,q = map(int,input().split())
a = list(map(int,input().split()))
a = [i-a[i]+1 for i in range(n)]
Q = [[] for _ in range(n)]
ans = [-1]*q
for _ in range(q):
L,R = map(int,input().split())
R = n-1-R
Q[R].append((L,_))
bit = [0]*(n+1)
def update(idx,val):
idx = n-1-idx
idx += 1
while idx < n+1:
bit[idx] += val
idx += idx&-idx
def getSum(idx):
idx = n-1-idx
idx += 1
ans = 0
while idx:
ans += bit[idx]
idx -= idx&-idx
return ans
for R in range(n):
if a[R] < 0 or getSum(0) < a[R]:
for L,i in Q[R]:
ans[i] = getSum(L)
continue
lo,hi = 0,R
while lo+1 < hi:
mid = (lo+hi)//2
if getSum(mid) < a[R]:
hi = mid
else:
lo = mid
if getSum(hi) >= a[R]:
update(hi,1)
else:
update(lo,1)
for L,i in Q[R]:
ans[i] = getSum(L)
print(*ans)
``` | output | 1 | 49,851 | 12 | 99,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,852 | 12 | 99,704 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
# 1 based indexing
class Fenwick:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def add(self, idx, val):
idx = int(idx)
while idx <= self.size:
self.tree[idx] += val
idx += idx & (-idx)
def sum(self, idx):
ret = 0
idx = int(idx)
while idx > 0:
ret += self.tree[idx]
idx -= idx & (-idx)
return ret
n, q = map(int, input().split())
A = [int(x) for x in input().split()]
A = [A[i] - (i + 1) for i in range(n)]
query = [[] for _ in range(n + 1)]
for i in range(q):
x, y = map(int, input().split())
l, r = x, n - y - 1
query[r].append((l, i))
ft = Fenwick(n + 1)
ans = [0 for _ in range(q + 3)]
for r in range(n):
el = A[r]
if el <= 0:
# checking for the first node as the no. of deletions is
# decreasing only
if ft.sum(1) >= -el:
low, high = 0, r
# low -> satisfies | high -> doesn't satifies
while low + 1 < high:
mid = (low + high) / 2
if ft.sum(mid + 1) >= -el:
low = mid
else : high = mid
ind = low
# Checking for the upperbound
if ft.sum(high + 1) >= -el:
ind = max(ind, high)
# Range Updating the Fenwick Tree
ft.add(1, 1)
ft.add(ind + 2, -1)
# Updating the answer for all the queries which are ending at 'r'
for qr in query[r]:
ans[qr[1]] = ft.sum(qr[0] + 1)
for _ in range(q):
print(ans[_])
``` | output | 1 | 49,852 | 12 | 99,705 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,853 | 12 | 99,706 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
class Fenwick:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def add(self, idx, val):
idx = int(idx)
while idx <= self.size:
self.tree[idx] += val
idx += idx & (-idx)
def sum(self, idx):
ret = 0
idx = int(idx)
while idx > 0:
ret += self.tree[idx]
idx -= idx & (-idx)
return ret
n, q = map(int, input().split())
A = [int(x) for x in input().split()]
A = [A[i] - (i + 1) for i in range(n)]
query = [[] for _ in range(n + 1)]
for i in range(q):
x, y = map(int, input().split())
l, r = x, n - y - 1
query[r].append((l, i))
ft = Fenwick(n + 1)
ans = [0 for _ in range(q + 3)]
for r in range(n):
el = A[r]
if el <= 0:
if ft.sum(1) >= -el:
low, high = 0, r
while low + 1 < high:
mid = (low + high) / 2
if ft.sum(mid + 1) >= -el:
low = mid
else : high = mid
ind = low
if ft.sum(high + 1) >= -el:
ind = max(ind, high)
ft.add(1, 1)
ft.add(ind + 2, -1)
for qr in query[r]:
ans[qr[1]] = ft.sum(qr[0] + 1)
for _ in range(q):
print(ans[_])
``` | output | 1 | 49,853 | 12 | 99,707 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,854 | 12 | 99,708 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
'''explained excellently in editorial'''
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
#binary lifting
def lower_bound(self, w):
#if w <= 0:
# return 0
x = 0
k = 1 << (self.size.bit_length() - 1)
while k:
if x + k <= self.size and self.tree[x + k] > w:
w -= self.tree[x + k]
x += k
k >>= 1
return x + 1
N, Q = map(int, input().split())
A = list(map(int, input().split()))
A = [i+1 - A[i] for i in range(N)]
query = [[] for _ in range(N+1)]
ans = [0] * Q
for q in range(Q):
x, y = map(int, input().split())
l = x+1
r = N-y
query[r].append((l, q))
bit = Bit(N+1)
for r in range(1, N+1):
a = A[r-1]
if a == 0:
bit.add(1, +1)
bit.add(r+1, -1)
elif a > 0:
#if the value of l is greater than k, it means that we cannot
#remove the particular number from the array, as there are not
#sufficient removals before it.
k = bit.lower_bound(a-1)
bit.add(1, +1)
bit.add(k, -1)
for l, q in query[r]:
ans[q] = bit.sum(l)
print(*ans, sep="\n")
if __name__ == '__main__':
main()
``` | output | 1 | 49,854 | 12 | 99,709 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,855 | 12 | 99,710 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
from bisect import *
from collections import *
from math import gcd,ceil,sqrt,floor,inf
from heapq import *
from itertools import *
from operator import add,mul,sub,xor,truediv,floordiv
from functools import *
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
#------------------------------------------------------------------------
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
farr=[1]
ifa=[]
def fact(x,mod=0):
if mod:
while x>=len(farr):
farr.append(farr[-1]*len(farr)%mod)
else:
while x>=len(farr):
farr.append(farr[-1]*len(farr))
return farr[x]
def ifact(x,mod):
global ifa
ifa.append(pow(farr[-1],mod-2,mod))
for i in range(x,0,-1):
ifa.append(ifa[-1]*i%mod)
ifa=ifa[::-1]
def per(i,j,mod=0):
if i<j: return 0
if not mod:
return fact(i)//fact(i-j)
return farr[i]*ifa[i-j]%mod
def com(i,j,mod=0):
if i<j: return 0
if not mod:
return per(i,j)//fact(j)
return per(i,j,mod)*ifa[j]%mod
def catalan(n):
return com(2*n,n)//(n+1)
def linc(f,t,l,r):
while l<r:
mid=(l+r)//2
if t>f(mid):
l=mid+1
else:
r=mid
return l
def rinc(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t<f(mid):
r=mid-1
else:
l=mid
return l
def ldec(f,t,l,r):
while l<r:
mid=(l+r)//2
if t<f(mid):
l=mid+1
else:
r=mid
return l
def rdec(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t>f(mid):
r=mid-1
else:
l=mid
return l
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def binfun(x):
c=0
for w in arr:
c+=ceil(w/x)
return c
def lowbit(n):
return n&-n
def inverse(a,m):
a%=m
if a<=1: return a
return ((1-inverse(m,a)*m)//a)%m
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
class smt:
def __init__(self,l,r,arr):
self.l=l
self.r=r
self.value=(1<<31)-1 if l<r else arr[l]
mid=(l+r)//2
if(l<r):
self.left=smt(l,mid,arr)
self.right=smt(mid+1,r,arr)
self.value&=self.left.value&self.right.value
#print(l,r,self.value)
def setvalue(self,x,val):
if(self.l==self.r):
self.value=val
return
mid=(self.l+self.r)//2
if(x<=mid):
self.left.setvalue(x,val)
else:
self.right.setvalue(x,val)
self.value=self.left.value&self.right.value
def ask(self,l,r):
if(l<=self.l and r>=self.r):
return self.value
val=(1<<31)-1
mid=(self.l+self.r)//2
if(l<=mid):
val&=self.left.ask(l,r)
if(r>mid):
val&=self.right.ask(l,r)
return val
class DSU:#容量+路径压缩
def __init__(self,n):
self.c=[-1]*n
def same(self,x,y):
return self.find(x)==self.find(y)
def find(self,x):
if self.c[x]<0:
return x
self.c[x]=self.find(self.c[x])
return self.c[x]
def union(self,u,v):
u,v=self.find(u),self.find(v)
if u==v:
return False
if self.c[u]<self.c[v]:
u,v=v,u
self.c[u]+=self.c[v]
self.c[v]=u
return True
def size(self,x): return -self.c[self.find(x)]
class UFS:#秩+路径
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
for v in graph[u]:
if v not in d or d[v]>d[u]+graph[u][v]:
d[v]=d[u]+graph[u][v]
heappush(heap,(d[v],v))
return d
def GP(it): return [(ch,len(list(g))) for ch,g in groupby(it)]
class DLN:
def __init__(self,val):
self.val=val
self.pre=None
self.next=None
t=1
for i in range(t):
n,q=RL()
a=RLL()
a=[i+1-a[i] for i in range(n)]
ans=[0]*q
res=[[] for i in range(1+n)]
for i in range(q):
x,y=RL()
l,r=x+1,n-y
res[r].append((i,l))
#print(a)
bi=BIT([0]*(n+1))
for r in range(1,n+1):
if a[r-1]>=0:
lo,hi=0,r
while lo<hi:
mid=(lo+hi+1)//2
tmp=bi.query(n-mid+1)
if tmp>=a[r-1]:
lo=mid
else:
hi=mid-1
if lo:
bi.update(n-lo+1,1)
for i,l in res[r]:
ans[i]=bi.query(n-l+1)
#print(r,ans,bi.arr)
for x in ans:
print(x)
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thread(target=main)
t.start()
t.join()
'''
``` | output | 1 | 49,855 | 12 | 99,711 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,856 | 12 | 99,712 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
from sys import stdin
def bitadd(a,w,bit):
x = a
while x <= (len(bit)-1):
bit[x] += w
x += x & (-1 * x)
def bitsum(a,bit):
ret = 0
x = a
while x > 0:
ret += bit[x]
x -= x & (-1 * x)
return ret
class RangeBIT:
def __init__(self,N,indexed):
self.bit1 = [0] * (N+2)
self.bit2 = [0] * (N+2)
self.mode = indexed
def bitadd(self,a,w,bit):
x = a
while x <= (len(bit)-1):
bit[x] += w
x += x & (-1 * x)
def bitsum(self,a,bit):
ret = 0
x = a
while x > 0:
ret += bit[x]
x -= x & (-1 * x)
return ret
def add(self,l,r,w):
l = l + (1-self.mode)
r = r + (1-self.mode)
self.bitadd(l,-1*w*l,self.bit1)
self.bitadd(r,w*r,self.bit1)
self.bitadd(l,w,self.bit2)
self.bitadd(r,-1*w,self.bit2)
def sum(self,l,r):
l = l + (1-self.mode)
r = r + (1-self.mode)
ret = self.bitsum(r,self.bit1) + r * self.bitsum(r,self.bit2)
ret -= self.bitsum(l,self.bit1) + l * self.bitsum(l,self.bit2)
return ret
n,q = map(int,stdin.readline().split());a = list(map(int,stdin.readline().split()));qs = [ [] for i in range(n+1) ];ans = [None] * q;BIT = [0] * (n+1)
for loop in range(q):x,y = map(int,stdin.readline().split());l = x+1;r = n-y;qs[r].append((l,loop))
for r in range(1,n+1):
b = r-a[r-1]
if b >= 0:
L = 1;R = r+1
while R-L != 1:
M = (L+R)//2
if bitsum(M,BIT) >= b:L = M
else:R = M
if bitsum(L,BIT) >= b:bitadd(1,1,BIT);bitadd(L+1,-1,BIT)
for ql,qind in qs[r]:ans[qind] = bitsum(ql,BIT)
for i in ans:print (i)
``` | output | 1 | 49,856 | 12 | 99,713 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5. | instruction | 0 | 49,857 | 12 | 99,714 |
Tags: binary search, constructive algorithms, data structures, greedy, two pointers
Correct Solution:
```
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
class BitSum:
def __init__(self, n):
self.n = n+1
self.table = [0]*self.n
def add(self, i, x):
i += 1
while i < self.n:
self.table[i] += x
i += i & -i
def sum(self, i):
i += 1
res = 0
while i > 0:
res += self.table[i]
i -= i & -i
return res
# 数列を度数分布とみたときに、x番目がどのインデックスにあるかを返す
# xが大きすぎるときは配列の長さnを返す
def rank(self, x):
idx = 0
for lv in range((self.n-1).bit_length()-1, -1, -1):
mid = idx+(1 << lv)
if mid >= self.n: continue
if self.table[mid] < x:
x -= self.table[mid]
idx += 1 << lv
return idx
# sum>=xとなる、最も右のindexを返す
def mostr(self, x):
idx = 0
for lv in range((self.n-1).bit_length()-1, -1, -1):
mid = idx+(1 << lv)
if mid >= self.n: continue
if self.table[mid] >= x:
x -= self.table[mid]
idx += 1 << lv
return idx-1
n, q = MI()
aa = LI1()
dd = [i-a for i, a in enumerate(aa)]
# print(dd)
rli = []
for i in range(q):
x, y = MI()
rli.append((n-y-1, x, i))
rli.sort()
# print(rli)
bit = BitSum(n+1)
k = 0
ans = [0]*q
for r, d in enumerate(dd):
j = -1
if d >= 0:
j = bit.mostr(d)
if j == n: j = r
bit.add(0, 1)
bit.add(j+1, -1)
while k < q and rli[k][0] == r:
r, l, i = rli[k]
k += 1
ans[i] = bit.sum(l)
# print(r,d,j,l,i,ans)
print(*ans, sep="\n")
``` | output | 1 | 49,857 | 12 | 99,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
# Fast IO (be careful about bytestring, not on interactive)
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
MaxLength = 2 ** 19
SegmentTree = []
CurLen = MaxLength
while CurLen != 0:
SegmentTree.append([0] * CurLen)
CurLen //= 2
Depth = len(SegmentTree)
def addElem(n,index): # add n to an element in index
for j in range(Depth):
if index < len(SegmentTree[j]):
SegmentTree[j][index] += n
index //= 2
def firstNSum(n): # Return the sum of the first n elements (0 to n-1)
nCpy = n
summ = 0
depthCur = 0
while nCpy != 0:
if nCpy % 2 != 0:
summ += SegmentTree[depthCur][nCpy - 1]
nCpy //= 2
depthCur += 1
return summ
n,q = map(int,input().split())
a = list(map(int,input().split()))
for i in range(n):
a[i] = i + 1 - a[i]
ans = [-1] * q
query = []
for i in range(q):
x,y = map(int,input().split())
query.append((i,x,n - y))
query.sort(key = lambda x: x[2])
currentY = 0
currentTotalSum = 0
for qu in query:
while currentY < qu[2]:
if a[currentY] >= 0 and currentTotalSum >= a[currentY]:
l = 0
r = currentY + 1
while l < r:
m = (l + r) // 2
if a[currentY] > currentTotalSum - firstNSum(m):
r = m
else:
l = m + 1
addElem(1,r-1)
currentTotalSum += 1
currentY += 1
ans[qu[0]] = currentTotalSum - firstNSum(qu[1])
for elem in ans:
print(elem)
``` | instruction | 0 | 49,858 | 12 | 99,716 |
Yes | output | 1 | 49,858 | 12 | 99,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
class Fenwick:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def add(self, idx, val):
idx = int(idx)
while idx <= self.size:
self.tree[idx] += val
idx += idx & (-idx)
def sum(self, idx):
ret = 0
idx = int(idx)
while idx > 0:
ret += self.tree[idx]
idx -= idx & (-idx)
return ret
n, q = map(int, input().split())
A = [int(x) for x in input().split()]
A = [A[i] - (i + 1) for i in range(n)]
query = [[] for _ in range(n + 1)]
for i in range(q):
x, y = map(int, input().split())
l, r = x, n - y - 1
query[r].append((l, i))
ft = Fenwick(n + 1)
# ans = [0 for _ in range(q + 3)]
# for r in range(n):
# ob = A[r]
# if ob <= 0:
# if ft.sum(1) >= -ob:
# low, high = 0, r
# while low + 1 < high:
# mid = low + high >> 1;
# if ft.sum(mid + 1) >= -ob:
# low = mid
# else: high = mid
# idx = low
# if ft.sum(high + 1) >= -ob:
# idx = max(idx, high)
# ft.add(1, 1)
# ft.add(idx + 2, -1)
# for qr in query[r]:
# ans[qr[1]] = ft.sum(qr[0] + 1)
#
# for _ in range(q):
# print(ans[_])
ans = [0 for _ in range(q + 3)]
for r in range(n):
ob = A[r]
if ob <= 0:
if ft.sum(1) >= -ob:
low, high = 0, r
while low + 1 < high:
mid = low + high >> 1
if ft.sum(mid + 1) >= -ob:
low = mid
else: high = mid
idx = high if ft.sum(high + 1) >= -ob else low
ft.add(1, 1)
ft.add(idx + 2, -1)
for qr in query[r]:
ans[qr[1]] = ft.sum(qr[0] + 1)
for _ in range(q):
print(ans[_])
``` | instruction | 0 | 49,859 | 12 | 99,718 |
Yes | output | 1 | 49,859 | 12 | 99,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
from sys import stdin
def bitadd(a,w,bit):
x = a
while x <= (len(bit)-1):bit[x] += w;x += x & (-1 * x)
def bitsum(a,bit):
ret = 0;x = a
while x > 0:ret += bit[x];x -= x & (-1 * x)
return ret
class RangeBIT:
def __init__(self,N,indexed):self.bit1 = [0] * (N+2);self.bit2 = [0] * (N+2);self.mode = indexed
def bitadd(self,a,w,bit):
x = a
while x <= (len(bit)-1):bit[x] += w;x += x & (-1 * x)
def bitsum(self,a,bit):
ret = 0;x = a
while x > 0:ret += bit[x];x -= x & (-1 * x)
return ret
def add(self,l,r,w):l = l + (1-self.mode);r = r + (1-self.mode);self.bitadd(l,-1*w*l,self.bit1);self.bitadd(r,w*r,self.bit1);self.bitadd(l,w,self.bit2);self.bitadd(r,-1*w,self.bit2)
def sum(self,l,r):l = l + (1-self.mode);r = r + (1-self.mode);ret = self.bitsum(r,self.bit1) + r * self.bitsum(r,self.bit2);ret -= self.bitsum(l,self.bit1) + l * self.bitsum(l,self.bit2);return ret
n,q = map(int,stdin.readline().split());a = list(map(int,stdin.readline().split()));qs = [ [] for i in range(n+1) ];ans = [None] * q;BIT = [0] * (n+1)
for loop in range(q):x,y = map(int,stdin.readline().split());l = x+1;r = n-y;qs[r].append((l,loop))
for r in range(1,n+1):
b = r-a[r-1]
if b >= 0:
L = 1;R = r+1
while R-L != 1:
M = (L+R)//2
if bitsum(M,BIT) >= b:L = M
else:R = M
if bitsum(L,BIT) >= b:bitadd(1,1,BIT);bitadd(L+1,-1,BIT)
for ql,qind in qs[r]:ans[qind] = bitsum(ql,BIT)
for i in ans:print (i)
``` | instruction | 0 | 49,860 | 12 | 99,720 |
Yes | output | 1 | 49,860 | 12 | 99,721 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
from sys import stdin
def bitadd(a,w,bit):
x = a
while x <= (len(bit)-1):
bit[x] += w
x += x & (-1 * x)
def bitsum(a,bit):
ret = 0
x = a
while x > 0:
ret += bit[x]
x -= x & (-1 * x)
return ret
class RangeBIT:
def __init__(self,N,indexed):
self.bit1 = [0] * (N+2)
self.bit2 = [0] * (N+2)
self.mode = indexed
def bitadd(self,a,w,bit):
x = a
while x <= (len(bit)-1):
bit[x] += w
x += x & (-1 * x)
def bitsum(self,a,bit):
ret = 0
x = a
while x > 0:
ret += bit[x]
x -= x & (-1 * x)
return ret
def add(self,l,r,w):
l = l + (1-self.mode)
r = r + (1-self.mode)
self.bitadd(l,-1*w*l,self.bit1)
self.bitadd(r,w*r,self.bit1)
self.bitadd(l,w,self.bit2)
self.bitadd(r,-1*w,self.bit2)
def sum(self,l,r):
l = l + (1-self.mode)
r = r + (1-self.mode)
ret = self.bitsum(r,self.bit1) + r * self.bitsum(r,self.bit2)
ret -= self.bitsum(l,self.bit1) + l * self.bitsum(l,self.bit2)
return ret
n,q = map(int,stdin.readline().split())
a = list(map(int,stdin.readline().split()))
qs = [ [] for i in range(n+1) ]
ans = [None] * q
for loop in range(q):
x,y = map(int,stdin.readline().split())
l = x+1
r = n-y
qs[r].append((l,loop))
BIT = [0] * (n+1)
for r in range(1,n+1):
b = r-a[r-1]
if b >= 0:
L = 1
R = r+1
while R-L != 1:
M = (L+R)//2
if bitsum(M,BIT) >= b:
L = M
else:
R = M
if bitsum(L,BIT) >= b:
bitadd(1,1,BIT)
bitadd(L+1,-1,BIT)
for ql,qind in qs[r]:
ans[qind] = bitsum(ql,BIT)
for i in ans:
print (i)
``` | instruction | 0 | 49,861 | 12 | 99,722 |
Yes | output | 1 | 49,861 | 12 | 99,723 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
a = input()
a = a.split(' ')
n = int(a[0])
q = int(a[1])
c = input()
c = c.split(' ')
for i in range(len(c)):
c[i] = int(c[i])
zapros = [[], []]
for i in range(q):
d = input()
d = d.split(' ')
zapros[0].append(int(d[0]))
zapros[1].append(int(d[1]))
l = []
for i in range(q):
l.append([])
for j in range(len(c)):
l[i].append(c[j])
for g in range(q):
number = n - 1 - zapros[0][g]+zapros[1][g]
if zapros[0][g] > 0:
for m in range(zapros[0][g]):
l[g][m] = n + 1
if zapros[1][g]>0:
for o in range(1, zapros[1][g]+1):
l[g][-(o)] = n+1
count = 0
for j in range(number):
for k in range(len(l[g])-3):
if l[g][-(k+1)]==(len(l[g])-k):
count +=1
l[g].remove(l[g][-(k+1)])
continue
print(count)
``` | instruction | 0 | 49,862 | 12 | 99,724 |
No | output | 1 | 49,862 | 12 | 99,725 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
import heapq
n,q = map(int,input().split())
a = list(map(int,input().split()))
C = [-1]*n
pq = []
bit = [0]*(n+1)
def update(idx,val):
idx += 1
while idx < n+1:
bit[idx] += val
idx += idx&-idx
def getSum(idx):
idx += 1
ans = 0
while idx:
ans += bit[idx]
idx -= idx&-idx
return ans
for i in range(n-1,-1,-1):
if a[i]-1 > i:
continue
heapq.heappush(pq,(i-a[i]+1,-i))
while pq and getSum(-pq[0][1]) >= pq[0][0]:
X = heapq.heappop(pq)
v = X[0]
idx = -X[1]
update(idx,1)
C[idx] = i
for _ in range(q):
L,R = map(int,input().split())
R = n-R
res = 0
for i in range(L,R):
if C[i] >= L:
res += 1
print(res)
``` | instruction | 0 | 49,863 | 12 | 99,726 |
No | output | 1 | 49,863 | 12 | 99,727 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
n,q=map(int,input().split())
b=list(map(int,input().split()))
f=1
l=[]
le=n
while f==1:
f=0
for j in range(le-1,-1,-1):
if j+1==b[j]:
b.pop(j)
f=1
l.append(j)
le=le-1
break
le=len(l)
for i in range(q):
s=0
x,y=map(int,input().split())
for j in range(le):
if l[j]>x-1 and l[j]<n-1-y:
s=s+1
elif l[j]<=x-1:
break
print(s)
``` | instruction | 0 | 49,864 | 12 | 99,728 |
No | output | 1 | 49,864 | 12 | 99,729 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated).
The weight of a is defined as the maximum number of elements you can remove.
You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a?
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array.
The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n).
Output
Print q lines, i-th line should contain a single integer — the answer to the i-th query.
Examples
Input
13 5
2 2 3 9 5 4 6 5 7 8 3 11 13
3 1
0 0
2 4
5 0
0 12
Output
5
11
6
1
0
Input
5 2
1 4 1 2 4
0 0
1 0
Output
2
0
Note
Explanation of the first query:
After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity).
Here is a strategy that removes 5 elements (the element removed is colored in red):
* [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×]
* [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×]
* [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×]
* [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×]
* [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×]
* [×, ×, ×, 9, 4, 5, 3, ×] (final state)
It is impossible to remove more than 5 elements, hence the weight is 5.
Submitted Solution:
```
#Python Template
from sys import stdin, stdout
def main():
canon_n, q = [int(i) for i in stdin.readline().split()]
canon_l = [int(i) for i in stdin.readline().split()]
for _ in range(q):
x, y = [int(i) for i in stdin.readline().split()]
l = canon_l.copy()
n = canon_n
weight = 0
while True:
for i in range(n-y, x, -1):
if l[i-1] == i:
weight += 1
l.remove(i)
break
else:
break
n = len(l)
stdout.write("{}\n".format(weight))
main()
``` | instruction | 0 | 49,865 | 12 | 99,730 |
No | output | 1 | 49,865 | 12 | 99,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. | instruction | 0 | 49,866 | 12 | 99,732 |
Tags: data structures, greedy
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
def prog():
n = int(input())
a = list(map(int,input().split()))
freq = [0]*101
for i in range(n):
freq[a[i]] += 1
mx = max(freq)
amt = freq.count(mx)
if amt >= 2:
print(n)
else:
must_appear = freq.index(mx)
ans = 0
for j in range(1,101):
if j == must_appear:
continue
first_idx = [10**6]*(n+1)
first_idx[0] = -1
curr = 0
for i in range(n):
if a[i] == must_appear:
curr += 1
elif a[i] == j:
curr -= 1
ans = max(ans, i - first_idx[curr])
first_idx[curr] = min(first_idx[curr],i)
print(ans)
prog()
``` | output | 1 | 49,866 | 12 | 99,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. | instruction | 0 | 49,867 | 12 | 99,734 |
Tags: data structures, greedy
Correct Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
# mod=1000000007
mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('0')
file = 1
def solve():
# for _ in range(ii()):
n = ii()
a = li()
b = Counter(a)
most_freq_elm,cnt = 0,1
for i in b:
if b[i] > b[most_freq_elm]:
most_freq_elm = i
cnt = 1
elif b[i] == b[most_freq_elm]:
cnt += 1
if cnt > 1:
print(n)
return
ans = 0
for i in b:
if i == most_freq_elm:
continue
prev_occur = [-2]*(2*n+1)
prev_occur[n] = -1
cnt = 0
# print(prev_occur)
for j in range(n):
if a[j] == i:
cnt += 1
elif a[j] == most_freq_elm:
cnt -= 1
if prev_occur[cnt+n]==-2:
prev_occur[cnt+n] = j
else:
ans = max(ans,j-prev_occur[cnt+n])
print(ans)
if __name__ =="__main__":
if(file):
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
``` | output | 1 | 49,867 | 12 | 99,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. | instruction | 0 | 49,868 | 12 | 99,736 |
Tags: data structures, greedy
Correct Solution:
```
import sys
from collections import defaultdict, Counter
import sys
import os
from io import BytesIO, IOBase
#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")
N = int(input())
arr = list(map(int, input().split()))
freq = Counter(arr)
ans = 0
fix = freq.most_common(1)[0][0]
for other in freq:
if other == fix:
continue
pref, last_pos = [0] * (N+2), {0:0}
for i in range(N):
now = 1 if arr[i] == other else 0
pref[i+1] = pref[i] + (-1 if arr[i] == fix else now)
last_pos.setdefault(pref[i+1], i+1)
ans = max(ans, i+1 - last_pos[pref[i+1]])
print(ans)
``` | output | 1 | 49,868 | 12 | 99,737 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. | instruction | 0 | 49,869 | 12 | 99,738 |
Tags: data structures, greedy
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
freq=[0]*(101)
for i in arr:freq[i]+=1
maxx=max(freq)
amtOFmaxx=freq.count(maxx)
if amtOFmaxx>=2:print(n)
else:
must_apper=freq.index(maxx)
ans=0
for j in range(1,101):
if j==must_apper:
continue
first_index=[10**6]*(n+1)
first_index[0]=-1
curr=0
for i in range(n):
if arr[i]==must_apper:
curr+=1
elif arr[i]==j:
curr-=1
ans=max(ans,i-first_index[curr])
first_index[curr]=min(first_index[curr],i)
#print(first_index)
print(ans)
``` | output | 1 | 49,869 | 12 | 99,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. | instruction | 0 | 49,870 | 12 | 99,740 |
Tags: data structures, greedy
Correct Solution:
```
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
cnt = Counter(A)
maxv = max(cnt.values())
x = [c for c in cnt if cnt[c] == maxv]
if len(x) > 1:
print(n)
else:
x = x[0]
ans = 0
for c in cnt:
if c == x: continue
dic = {0: -1}
cur = 0
tmp = 0
for i, a in enumerate(A):
cur += 1 if a == c else -1 if a == x else 0
if cur in dic:
tmp = max(tmp, i - dic[cur])
dic.setdefault(cur, i)
ans = max(ans, tmp)
print(ans)
``` | output | 1 | 49,870 | 12 | 99,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times. | instruction | 0 | 49,871 | 12 | 99,742 |
Tags: data structures, greedy
Correct Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *
from math import sqrt, pi, ceil, log, inf,gcd
from itertools import permutations
from copy import deepcopy
from heapq import *
def main():
n=int(input())
a=[0]*(101)
b=list(map(int,input().split()))
for i in b:
a[i]+=1
ma=max(a)
if a.count(ma)>=2:
print(n)
else:
ele=a.index(ma)
maa=0
for i in range(1,101):
if i!=ele:
c=Counter()
su=0
for j in range(n):
if b[j]==i:
su-=1
elif b[j]==ele:
su+=1
if c[su]==0:
c[su]=j+1
else:
maa=max(maa,j+1-c[su])
if su==0:
maa=max(maa,j+1)
print(maa)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | output | 1 | 49,871 | 12 | 99,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
freq=[0]*(101)
for i in arr:
freq[i]+=1
freq2=[0]*(101)
ans=0
k1=3
j=0
for i in range(n):
freq2[arr[i]]+=1
if i>=k1-1:
freq3=[]
for k in range(1,101):
if freq2[k]>=1:
freq3.append(freq2[k])
#print(freq3)
if freq3.count(max(freq3))>=3:
ans=max(ans,i+1)
if ans==n or ans==n-1:
print(ans)
exit()
freq2=[0]*101
for j in range(n):
freq2[arr[j]]+=1
freq3=[]
for k in range(1,101):
if freq[k]>=1:freq3.append(freq[k]-freq2[k])
#print(freq3,j+1)
if max(freq3)>0 and freq3.count(max(freq3))>=2:
ans=max(ans,n-j-1)
#print()
break
print(ans)
``` | instruction | 0 | 49,872 | 12 | 99,744 |
No | output | 1 | 49,872 | 12 | 99,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
freq=[0]*(101)
for i in arr:
freq[i]+=1
freq2=[0]*(101)
ans=0
k1=3
j=0
for i in range(n):
freq2[arr[i]]+=1
if i>=1:
freq3=[]
for k in range(1,101):
if freq2[k]>=1:
freq3.append(freq2[k])
#print(freq3)
if freq3.count(max(freq3))>=3:
ans=max(ans,i+1)
freq2=[0]*101
for j in range(n):
freq2[arr[j]]+=1
freq3=[]
for k in range(1,101):
if freq[k]>=1:freq3.append(freq[k]-freq2[k])
#print(freq3,j+1)
if max(freq3)>0 and freq3.count(max(freq3))>=2:
ans=max(ans,n-j-1)
#print()
break
print(ans)
``` | instruction | 0 | 49,873 | 12 | 99,746 |
No | output | 1 | 49,873 | 12 | 99,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
freq=[0]*(101)
for i in arr:
freq[i]+=1
freq2=[0]*(101)
ans=0
k1=3
j=0
for i in range(n):
freq2[arr[i]]+=1
if i>=k1-1:
freq3=[]
for k in range(1,101):
if freq2[k]>=1:
freq3.append(freq2[k])
#print(freq3)
if freq3.count(max(freq3))>=3:
ans=max(ans,i+1)
freq2=[0]*101
for j in range(n):
freq2[arr[j]]+=1
freq3=[]
for k in range(1,101):
if freq[k]>=1:freq3.append(freq[k]-freq2[k])
#print(freq3,j+1)
if max(freq3)>0 and freq3.count(max(freq3))>=2:
ans=max(ans,n-j-1)
#print()
break
print(ans)
``` | instruction | 0 | 49,874 | 12 | 99,748 |
No | output | 1 | 49,874 | 12 | 99,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.
You are given an array [a_1, a_2, ..., a_n].
Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs f times in this subarray, then at least 2 different values should occur exactly f times.
An array c is a subarray of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ min(n, 100)) — elements of the array.
Output
You should output exactly one integer — the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output 0.
Examples
Input
7
1 1 2 2 3 3 3
Output
6
Input
10
1 1 1 5 4 1 3 1 2 2
Output
7
Input
1
1
Output
0
Note
In the first sample, the subarray [1, 1, 2, 2, 3, 3] is good, but [1, 1, 2, 2, 3, 3, 3] isn't: in the latter there are 3 occurrences of number 3, and no other element appears 3 times.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
freq=[0]*(200)
ans=0
for i in range(n):
freq[arr[i]]+=1
freq3=[]
for k in range(200):
if freq[k]>0:freq3.append(freq[k])
#print(freq3)
if freq3.count(max(freq3))>=2:
ans=max(ans,i+1)
freq2=[0]*(200)
for i in range(n):
freq2[arr[i]]+=1
freq3=[]
for k in range(200):
if freq[k]>0:freq3.append(freq[k]-freq2[k])
#print(freq3,j+1)
if freq3.count(max(freq3))>=2:
ans=max(ans,n-i-1)
#print()
break
print(ans)
``` | instruction | 0 | 49,875 | 12 | 99,750 |
No | output | 1 | 49,875 | 12 | 99,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>. | instruction | 0 | 50,022 | 12 | 100,044 |
Tags: data structures
Correct Solution:
```
import sys
class SegmTree():
def __init__(self, array=None):
size = len(array)
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [0] * (2*self.N)
for i in range(size):
self.tree[i+self.N] = array[i]
self.build()
def build(self):
for i in range(self.N - 1, 0, -1):
self.tree[i] = self.tree[i<<1] + self.tree[i<<1|1]
def add(self, i, value=1):
i += self.N
while i > 0:
self.tree[i] += value
i >>= 1
def get_sum(self, l, r):
N = self.N
l += N
r += N
result = 0
while l < r:
if l & 1:
result += self.tree[l]
l += 1
if r & 1:
r -= 1
result += self.tree[r]
l >>= 1
r >>= 1
return result
def find_kth_nonzero(self, k):
i = 1
if k < 1 or k > self.tree[1]:
return -1
while i < self.N:
i <<= 1
if self.tree[i] < k:
k -= self.tree[i]
i |= 1
return i - self.N
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ord_p = [0] * n
ord_q = [0] * n
st = SegmTree([1] * n)
for i, val in enumerate(p):
ord_p[i] = st.get_sum(0, val)
st.add(val, -1)
st = SegmTree([1] * n)
for i, val in enumerate(q):
ord_q[i] = st.get_sum(0, val)
st.add(val, -1)
transfer = 0
for i in range(n-1, -1, -1):
radix = n-i
ord_p[i] = ord_p[i] + ord_q[i] + transfer
if ord_p[i] < radix:
transfer = 0
else:
transfer = 1
ord_p[i] -= radix
st = SegmTree([1] * n)
for i in range(n):
k = ord_p[i] + 1
ord_q[i] = st.find_kth_nonzero(k)
st.add(ord_q[i], -1)
print(*ord_q)
``` | output | 1 | 50,022 | 12 | 100,045 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>. | instruction | 0 | 50,023 | 12 | 100,046 |
Tags: data structures
Correct Solution:
```
def sum(BIT, i):
s = 0
while i > 0:
s += BIT[i]
i -= i & (-i)
return s
def update(BIT, i, v):
while i < len(BIT):
BIT[i] += v
i += i & (-i)
def find(fen, k):
curr = 0
ans = 0
prevsum = 0
for i in range(19, -1, -1):
if ((curr + (1 << i) < n) and fen[curr + (1 << i)] + prevsum < k):
ans = curr + (1 << i)
curr = ans
prevsum += fen[curr]
return ans + 1
def Rank(x,BIT) :
return sum(BIT,x)
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
factp = []
factq = []
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in p:
factp.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in q:
factq.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
carry = 0
for i in range(n - 1, -1, -1):
radix = n - i
factp[i] = factp[i] + factq[i] + carry
if factp[i] < radix:
carry = 0
else:
carry = 1
factp[i] -= radix
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
res=[]
for i in range(n):
k = factp[i]+1
res.append(find(BIT,k)-1)
update(BIT,res[-1]+1,-1)
print(*res)
``` | output | 1 | 50,023 | 12 | 100,047 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>. | instruction | 0 | 50,024 | 12 | 100,048 |
Tags: data structures
Correct Solution:
```
import sys
class SegmTree():
def __init__(self, array=None):
size = len(array)
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [0] * (2*self.N)
for i in range(size):
self.tree[i+self.N] = array[i]
self.build()
def build(self):
for i in range(self.N - 1, 0, -1):
self.tree[i] = self.tree[i<<1] + self.tree[i<<1|1]
def add(self, i, value=1):
i += self.N
while i > 0:
self.tree[i] += value
i >>= 1
def get_sum(self, l, r):
N = self.N
l += N
r += N
result = 0
while l < r:
if l & 1:
result += self.tree[l]
l += 1
if r & 1:
r -= 1
result += self.tree[r]
l >>= 1
r >>= 1
return result
def find_kth_nonzero(self, k):
i = 1
if k < 1 or k > self.tree[1]:
return -1
while i < self.N:
i <<= 1
if self.tree[i] < k:
k -= self.tree[i]
i |= 1
return i - self.N
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ord_p = []
ord_q = []
st = SegmTree([1] * n)
for i, val in enumerate(p):
ord_p.append(st.get_sum(0, val))
st.add(val, -1)
st = SegmTree([1] * n)
for i, val in enumerate(q):
ord_q.append(st.get_sum(0, val))
st.add(val, -1)
transfer = 0
for i in range(n-1, -1, -1):
radix = n-i
ord_p[i] = ord_p[i] + ord_q[i] + transfer
if ord_p[i] < radix:
transfer = 0
else:
transfer = 1
ord_p[i] -= radix
st = SegmTree([1] * n)
for i in range(n):
k = ord_p[i] + 1
ord_q[i] = st.find_kth_nonzero(k)
st.add(ord_q[i], -1)
print(*ord_q)
``` | output | 1 | 50,024 | 12 | 100,049 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.
For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)
Misha has two permutations, p and q. Your task is to find their sum.
Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.
Input
The first line contains an integer n (1 ≤ n ≤ 200 000).
The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.
The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.
Output
Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.
Examples
Input
2
0 1
0 1
Output
0 1
Input
2
0 1
1 0
Output
1 0
Input
3
1 2 0
2 1 0
Output
1 0 2
Note
Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).
In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.
In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.
Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).
In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>.
Submitted Solution:
```
def sum(BIT, i):
s = 0
while i > 0:
s += BIT[i]
i -= i & (-i)
return s
def update(BIT, i, v):
while i < len(BIT):
BIT[i] += v
i += i & (-i)
def find(fen, k):
curr = 0
ans = 0
prevsum = 0
for i in range(19, -1, -1):
if ((curr + (1 << i) < n) and fen[curr + (1 << i)] + prevsum < k):
ans = curr + (1 << i)
curr = ans
prevsum += fen[curr]
return ans + 1
def Rank(x,BIT) :
return sum(BIT,x)
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
factp = []
factq = []
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in p:
factp.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
for val in q:
factq.append(Rank(val+1,BIT)-1)
update(BIT,val+1,-1)
carry = 0
for i in range(n - 1, -1, -1):
radix = n - i
factp[i] = factp[i] + factq[i] + carry
if factp[i] < radix:
carru = 0
else:
carry = 1
factp[i] -= radix
BIT = [0] * (n + 1)
for j in range(n):
update(BIT,j+1,1)
res=[]
for i in range(n):
k = factp[i]+1
res.append(find(BIT,k)-1)
update(BIT,res[-1]+1,-1)
print(*res)
``` | instruction | 0 | 50,025 | 12 | 100,050 |
No | output | 1 | 50,025 | 12 | 100,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n integers a1, a2, ..., an.
Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poorness of a segment is defined as the absolute value of sum of the elements of segment.
Input
The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).
Output
Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
3
1 2 3
Output
1.000000000000000
Input
4
1 2 3 4
Output
2.000000000000000
Input
10
1 10 2 9 3 8 4 7 5 6
Output
4.500000000000000
Note
For the first case, the optimal value of x is 2 so the sequence becomes - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case.
For the second sample the optimal value of x is 2.5 so the sequence becomes - 1.5, - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case. | instruction | 0 | 50,058 | 12 | 100,116 |
Tags: ternary search
Correct Solution:
```
n = int(input())
A = list(map(int, input().split()))
def max_subarray(A):
max_ending_here = max_so_far = 0
for x in A:
max_ending_here = max(0, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
def f1(x):
return max_subarray(a-x for a in A)
def f2(x):
return max_subarray(-a+x for a in A)
m = max(abs(a) for a in A)
l, r = -m, m
for _ in range(100):
mid = (l+r) / 2
v1, v2 = f1(mid), f2(mid)
if abs(v1 - v2) < 1e-8:
break
elif v1 > v2:
l = mid
else:
r = mid
print(v1)
``` | output | 1 | 50,058 | 12 | 100,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n integers a1, a2, ..., an.
Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poorness of a segment is defined as the absolute value of sum of the elements of segment.
Input
The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).
Output
Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
3
1 2 3
Output
1.000000000000000
Input
4
1 2 3 4
Output
2.000000000000000
Input
10
1 10 2 9 3 8 4 7 5 6
Output
4.500000000000000
Note
For the first case, the optimal value of x is 2 so the sequence becomes - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case.
For the second sample the optimal value of x is 2.5 so the sequence becomes - 1.5, - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case. | instruction | 0 | 50,059 | 12 | 100,118 |
Tags: ternary search
Correct Solution:
```
def max_sum(nums, shift):
res = 0
res_m = 0
cur_sum = 0
cur_m_sum = 0
for i in range(len(nums)):
cur_sum += (nums[i] + shift)
cur_m_sum += (nums[i] + shift)
res = max(res, cur_sum)
cur_sum = max(0, cur_sum)
res_m = min(res_m, cur_m_sum)
cur_m_sum = min(0, cur_m_sum)
return res, -res_m
def weaks(nums, shift):
return max_sum(nums, shift)
def main():
int(input())
nums = list(map(int, input().split()))
l = -10000
r = 10000
ans = max(weaks(nums, 0))
w1 = 1
w2 = -1
PREC = 10**-6
while abs(w1 - w2) >= PREC and abs(w1 - w2) > PREC * max(w1, w2):
m = (r + l)/2
# print (w1,w2,r,l,m)
w1, w2 = weaks(nums, m)
# print(w1, w2)
if w1 > w2:
r = m
else:
l = m
print ((w1 + w2) / 2)
# print (weaks([1,2,3],-2500.0))
main()
``` | output | 1 | 50,059 | 12 | 100,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence of n integers a1, a2, ..., an.
Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poorness of a segment is defined as the absolute value of sum of the elements of segment.
Input
The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.
The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).
Output
Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
3
1 2 3
Output
1.000000000000000
Input
4
1 2 3 4
Output
2.000000000000000
Input
10
1 10 2 9 3 8 4 7 5 6
Output
4.500000000000000
Note
For the first case, the optimal value of x is 2 so the sequence becomes - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case.
For the second sample the optimal value of x is 2.5 so the sequence becomes - 1.5, - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case. | instruction | 0 | 50,060 | 12 | 100,120 |
Tags: ternary search
Correct Solution:
```
def main():
input()
a = list(map(int, input().split()))
def f(a):
maxend = maxnow = 0
for x in a:
maxend = max(0, maxend + x)
maxnow = max(maxnow, maxend)
return maxnow
f1 = lambda x: f(i - x for i in a)
f2 = lambda x: f(x - i for i in a)
Max = max(abs(i) for i in a)
L, R = -Max, Max
eps = 10 ** -8
for i in range(100):
m = (L + R) / 2
v1, v2 = f1(m), f2(m)
if abs(v1 - v2) < eps:
break
if v1 > v2:
L = m
else:
R = m
print(v1)
main()
``` | output | 1 | 50,060 | 12 | 100,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 ≤ n ≤ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^6) — the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,559 | 12 | 101,118 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
from math import *
n = int(input())
l = list(map(int,input().split()))
ans = 0
l.sort()
d = dict()
for i in l:
if i not in d:
d[i] = 1
else:
d[i] += 1
for key,val in d.items():
if val == 2*n:
ans = -1
if(ans != -1):
for i in l:
print(i,end = " ")
print()
else:
print(ans)
``` | output | 1 | 50,559 | 12 | 101,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 ≤ n ≤ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^6) — the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,560 | 12 | 101,120 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
if a[0] == a[-1]:
print(-1)
else:
for i in a:
print(i, end=' ')
``` | output | 1 | 50,560 | 12 | 101,121 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.