message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 β€ u < v β€ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions:
* all the pairs in the list are distinct and form an inversion in a.
* all the pairs that form an inversion in a are in the list.
* Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the array.
Next line contains n integers a_1,a_2,...,a_n (1 β€ a_i β€ 10^9) β elements of the array.
Output
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0 β€ m β€ (n(n-1))/(2)) β number of pairs in the list.
The i-th of the following m lines should contain two integers u_i, v_i (1 β€ u_i < v_iβ€ n).
If there are multiple possible answers, you may find any of them.
Examples
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
Note
In the first sample test case the array will change in this order [3,1,2] β [2,1,3] β [1,2,3].
In the second sample test case it will be [1,8,1,6] β [1,6,1,8] β [1,1,6,8].
In the third sample test case the array is already sorted.
Submitted Solution:
```
import collections
import bisect
n=int(input())
arr=list(map(int,input().split()))
persuit=sorted(arr)
goal=[0]*n
cnt=collections.defaultdict(int)
for i in range(n):
index=bisect.bisect_left(persuit,arr[i])
index+=cnt[arr[i]]
cnt[arr[i]]+=1
goal[i]=index
indexs=[0]*n
for i in range(n):
indexs[goal[i]]=i
def is_sort(arr):
n=len(arr)
for i in range(n-1):
if arr[i+1]-arr[i]<0:
return False
return True
ans=[]
while not is_sort(indexs):
for i in range(n-1):
if indexs[i]>indexs[i+1]:
ans.append((indexs[i+1],indexs[i]))
indexs[i],indexs[i+1]=indexs[i+1],indexs[i]
print(len(ans))
for u,v in ans:
print(u+1,v+1)
``` | instruction | 0 | 25,759 | 12 | 51,518 |
Yes | output | 1 | 25,759 | 12 | 51,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 β€ u < v β€ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions:
* all the pairs in the list are distinct and form an inversion in a.
* all the pairs that form an inversion in a are in the list.
* Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the array.
Next line contains n integers a_1,a_2,...,a_n (1 β€ a_i β€ 10^9) β elements of the array.
Output
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0 β€ m β€ (n(n-1))/(2)) β number of pairs in the list.
The i-th of the following m lines should contain two integers u_i, v_i (1 β€ u_i < v_iβ€ n).
If there are multiple possible answers, you may find any of them.
Examples
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
Note
In the first sample test case the array will change in this order [3,1,2] β [2,1,3] β [1,2,3].
In the second sample test case it will be [1,8,1,6] β [1,6,1,8] β [1,1,6,8].
In the third sample test case the array is already sorted.
Submitted Solution:
```
import math,sys,bisect
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
#sys.setrecursionlimit(1000000)
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda: map(int,input().split())
alele = lambda: list(map(int, input().split()))
#def list2d(a, b, c): return [[c] * b for i in range(a)]
#def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
#MOD = 1000000000 + 7
def Y(c): print(["NO","YES"][c])
def y(c): print(["no","yes"][c])
def Yy(c): print(["No","Yes"][c])
N = int(input())
A = alele()
B = sorted([(A[i],i) for i in range(N)],reverse = True)
Ans = []
for i,j in B:
for k in range(j):
if i<A[k]:
Ans.append((k+1,j+1))
print(len(Ans))
for i,j in Ans:
print(i,j)
``` | instruction | 0 | 25,760 | 12 | 51,520 |
Yes | output | 1 | 25,760 | 12 | 51,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 β€ u < v β€ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions:
* all the pairs in the list are distinct and form an inversion in a.
* all the pairs that form an inversion in a are in the list.
* Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the array.
Next line contains n integers a_1,a_2,...,a_n (1 β€ a_i β€ 10^9) β elements of the array.
Output
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0 β€ m β€ (n(n-1))/(2)) β number of pairs in the list.
The i-th of the following m lines should contain two integers u_i, v_i (1 β€ u_i < v_iβ€ n).
If there are multiple possible answers, you may find any of them.
Examples
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
Note
In the first sample test case the array will change in this order [3,1,2] β [2,1,3] β [1,2,3].
In the second sample test case it will be [1,8,1,6] β [1,6,1,8] β [1,1,6,8].
In the third sample test case the array is already sorted.
Submitted Solution:
```
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def issorted(a):
for i in range(1,len(a)):
if a[i-1] > a[i]:
return False
return True
n = mint()
a = list(mints())
inv = []
for i in range(1,n):
for j in range(i):
if a[i] < a[j]:inv.append((i,-a[j],-j))
inv.sort(reverse=True)
r = list(range(len(inv)))
if r is not None:
print(len(r))
for z in r:v, _, u = inv[z];u = -u;print(u+1,v+1)
else:
print("wut")
``` | instruction | 0 | 25,761 | 12 | 51,522 |
Yes | output | 1 | 25,761 | 12 | 51,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 β€ u < v β€ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions:
* all the pairs in the list are distinct and form an inversion in a.
* all the pairs that form an inversion in a are in the list.
* Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the array.
Next line contains n integers a_1,a_2,...,a_n (1 β€ a_i β€ 10^9) β elements of the array.
Output
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0 β€ m β€ (n(n-1))/(2)) β number of pairs in the list.
The i-th of the following m lines should contain two integers u_i, v_i (1 β€ u_i < v_iβ€ n).
If there are multiple possible answers, you may find any of them.
Examples
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
Note
In the first sample test case the array will change in this order [3,1,2] β [2,1,3] β [1,2,3].
In the second sample test case it will be [1,8,1,6] β [1,6,1,8] β [1,1,6,8].
In the third sample test case the array is already sorted.
Submitted Solution:
```
from heapq import *
import sys
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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def solve():
ans = []
for i in range(n - 1, 0, -1):
hp = []
for j in range(i - 1, -1, -1):
if aa[j] > aa[i]:
heappush(hp, (aa[j], j))
while hp:
a, j = heappop(hp)
aa[j] = aa[i]
aa[i] = a
ans.append((j, i))
print(len(ans))
for i, j in ans: print(i + 1, j + 1)
n=II()
aa=LI()
solve()
``` | instruction | 0 | 25,762 | 12 | 51,524 |
No | output | 1 | 25,762 | 12 | 51,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 β€ u < v β€ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions:
* all the pairs in the list are distinct and form an inversion in a.
* all the pairs that form an inversion in a are in the list.
* Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the array.
Next line contains n integers a_1,a_2,...,a_n (1 β€ a_i β€ 10^9) β elements of the array.
Output
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0 β€ m β€ (n(n-1))/(2)) β number of pairs in the list.
The i-th of the following m lines should contain two integers u_i, v_i (1 β€ u_i < v_iβ€ n).
If there are multiple possible answers, you may find any of them.
Examples
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
Note
In the first sample test case the array will change in this order [3,1,2] β [2,1,3] β [1,2,3].
In the second sample test case it will be [1,8,1,6] β [1,6,1,8] β [1,1,6,8].
In the third sample test case the array is already sorted.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
b = sorted(a)
c=[]
for i in range(n-1):
if a[i]!=b[i]:
for j in range(i+1,n):
if a[j]==b[i]:
ind=j
break
c.append([i+1,ind+1])
temp=a[i]
a[i]=a[ind]
a[ind]=temp
print(len(c))
for i in range(len(c)):
print(c[i][1],c[i][0])
``` | instruction | 0 | 25,763 | 12 | 51,526 |
No | output | 1 | 25,763 | 12 | 51,527 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 β€ u < v β€ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions:
* all the pairs in the list are distinct and form an inversion in a.
* all the pairs that form an inversion in a are in the list.
* Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the array.
Next line contains n integers a_1,a_2,...,a_n (1 β€ a_i β€ 10^9) β elements of the array.
Output
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0 β€ m β€ (n(n-1))/(2)) β number of pairs in the list.
The i-th of the following m lines should contain two integers u_i, v_i (1 β€ u_i < v_iβ€ n).
If there are multiple possible answers, you may find any of them.
Examples
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
Note
In the first sample test case the array will change in this order [3,1,2] β [2,1,3] β [1,2,3].
In the second sample test case it will be [1,8,1,6] β [1,6,1,8] β [1,1,6,8].
In the third sample test case the array is already sorted.
Submitted Solution:
```
print("2")
print("1 2")
print("2 3")
``` | instruction | 0 | 25,764 | 12 | 51,528 |
No | output | 1 | 25,764 | 12 | 51,529 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 β€ u < v β€ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with the following conditions:
* all the pairs in the list are distinct and form an inversion in a.
* all the pairs that form an inversion in a are in the list.
* Starting from the given array, if you swap the values at indices u_1 and v_1, then the values at indices u_2 and v_2 and so on, then after all pairs are processed, the array a will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the array.
Next line contains n integers a_1,a_2,...,a_n (1 β€ a_i β€ 10^9) β elements of the array.
Output
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0 β€ m β€ (n(n-1))/(2)) β number of pairs in the list.
The i-th of the following m lines should contain two integers u_i, v_i (1 β€ u_i < v_iβ€ n).
If there are multiple possible answers, you may find any of them.
Examples
Input
3
3 1 2
Output
2
1 3
1 2
Input
4
1 8 1 6
Output
2
2 4
2 3
Input
5
1 1 1 2 2
Output
0
Note
In the first sample test case the array will change in this order [3,1,2] β [2,1,3] β [1,2,3].
In the second sample test case it will be [1,8,1,6] β [1,6,1,8] β [1,1,6,8].
In the third sample test case the array is already sorted.
Submitted Solution:
```
import math
am = int(input())
arr = list(map(int,input().split()))
pairs = []
def f(el):
return el[3]
def s(el):
return el[2]
for i in range(am):
for g in range(i,am):
if arr[i] > arr[g]:
pairs.append([i,g,arr[i], arr[g]])
pairs.sort(key=s)
pairs.reverse()
pairs.sort(key=f)
pairs.reverse()
# for i in range(len(pairs)):
# arr[pairs[i][0]],arr[pairs[i][1]] = arr[pairs[i][1]],arr[pairs[i][0]]
# print(arr)
if (len(pairs)):
print(len(pairs))
for i in range(len(pairs)):
print(pairs[i][0] + 1, pairs[i][1] + 1)
else:
print(0)
``` | instruction | 0 | 25,765 | 12 | 51,530 |
No | output | 1 | 25,765 | 12 | 51,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,766 | 12 | 51,532 |
Tags: greedy, sortings
Correct Solution:
```
n=int(input())
for i in range(0,n):
l=int(input())
l1=list(map(int,input().split()))
f=0
m=sorted(l1)
for k in range(1,l):
if m[k]-m[k-1]>1:
print("NO")
f=1
break
if f==0:
print("YES")
``` | output | 1 | 25,766 | 12 | 51,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,767 | 12 | 51,534 |
Tags: greedy, sortings
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = [int(ele) for ele in input().split()]
a.sort()
i = 0
while i < n-1:
if abs(a[i] - a[i+1]) <= 1 :
if a[i] < a[i + 1]:
a.remove(a[i])
else:
a.remove(a[i+1])
i -= 1
n -= 1
i += 1
if len(a) == 1:
print("YES")
else:
print("NO")
``` | output | 1 | 25,767 | 12 | 51,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,768 | 12 | 51,536 |
Tags: greedy, sortings
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
if len(arr) == 1 or len(set(arr)) == 1:
print("YES")
else:
max = 0
for i in range(len(arr)-1):
current_max = arr[i+1]-arr[i]
if current_max > max:
max = current_max
if max > 1:
print("NO")
else:
print("YES")
``` | output | 1 | 25,768 | 12 | 51,537 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,769 | 12 | 51,538 |
Tags: greedy, sortings
Correct Solution:
```
"""609C"""
# import math
def main():
n = int(input())
a = list(map(int,input().split()))
cnte=0
cnto=0
for i in range(len(a)):
if a[i]%2==0:
cnte+=1
else:
cnto+=1
if sum(a)%2!=0:
print("YES")
elif cnte>0 and cnto>0:
print("YES")
else:
print("NO")
return
# main()
def test():
t= int(input())
while t:
main()
t-=1
test()
``` | output | 1 | 25,769 | 12 | 51,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,770 | 12 | 51,540 |
Tags: greedy, sortings
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
e=0
o=0
d = list(map(int, input().split()))
for i in d:
if i%2:
e+=1
else:
o+=1
if o==n or (o==0 and n%2==0):
print("NO")
else:
print("YES")
``` | output | 1 | 25,770 | 12 | 51,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,771 | 12 | 51,542 |
Tags: greedy, sortings
Correct Solution:
```
t = int(input())
for i in range(t):
_sum = 0
div = 0
n = int(input())
for e in map(int, input().split()):
_sum += e
if e % 2 == 0:
div += 1
if _sum % 2 or (div > 1 and n - div >= 1) or (n - div > 1 and div >= 1):
print('YES')
else:
print('NO')
``` | output | 1 | 25,771 | 12 | 51,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,772 | 12 | 51,544 |
Tags: greedy, sortings
Correct Solution:
```
import sys
for i in range(int(input())):
a = int(input())
b = list(map(int, input().split()))
b.sort()
for i in range(len(b)-1):
if b[i+1] - b[i] > 1:
print("NO")
break
else:
print("YES")
``` | output | 1 | 25,772 | 12 | 51,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4. | instruction | 0 | 25,773 | 12 | 51,546 |
Tags: greedy, sortings
Correct Solution:
```
#!/usr/bin/pypy3
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
f = True
for i in range(1, n):
if (a[i] - a[i - 1]) > 1:
f = False
print('YES' if f is True else 'NO')
``` | output | 1 | 25,773 | 12 | 51,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
def q():
n = int(input())
a = input().split(" ")
a_o = []
for i in a:
if int(i) % 2 != 0:
a_o = a_o + [i]
else:
pass
if (len(a_o) == n and len(a_o) % 2 == 0) or len(a_o) == 0:
print("NO")
else:
print("YES")
t = int(input())
for i in range(t):
q()
``` | instruction | 0 | 25,774 | 12 | 51,548 |
Yes | output | 1 | 25,774 | 12 | 51,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
A =sorted(list(map(int,input().split())))
for i in range(1,n):
if A[i]-A[i-1]>1:
print('NO')
break
else:
print('YES')
``` | instruction | 0 | 25,775 | 12 | 51,550 |
Yes | output | 1 | 25,775 | 12 | 51,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
arr=[int(i) for i in input().split()]
boo=True
arr.sort()
for i in range(n-1):
if abs(arr[i]-arr[i+1])>1:
print("NO")
boo=False
break
if boo:
print("YES")
``` | instruction | 0 | 25,776 | 12 | 51,552 |
Yes | output | 1 | 25,776 | 12 | 51,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
t = int(input())
for cases in range(t):
n = int(input())
arr = [int(num) for num in input().split(' ')]
arr.sort()
c = 0
for i in range(n-1):
j = i + 1
if arr[i]+1 in arr[j:n] or arr[i] in arr[j:n]:
continue
else:
c = 1
break
if c==1:
print('NO')
else:
print('YES')
``` | instruction | 0 | 25,777 | 12 | 51,554 |
Yes | output | 1 | 25,777 | 12 | 51,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
test_cases = int(input())
for test in range(test_cases):
ans = False
n = int(input())
arr = list(map(int , input().strip().split()))
odd_arr = [i for i in arr if i%2 != 0]
odds = len(odd_arr)
if sum(arr)%2 != 0: ans = True
elif odds == 0: print('NO')
elif max(odd_arr) == min(odd_arr) and odds % 2 == 0: print('NO')
else:
t = arr
for i in range(len(odd_arr)):
t[i] = odd_arr[i]
if sum(odd_arr)%2 != 0:
ans = True
break
else : t = arr
if ans is True:print('YES')
else: print('NO')
``` | instruction | 0 | 25,778 | 12 | 51,556 |
No | output | 1 | 25,778 | 12 | 51,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
def solve():
T=int(input())
for t in range(T):
n=int(input())
ip=input().split()
a=[int(x) for x in ip]
e,o=0,0
for i in range(n):
if a[i]%2==0:
e+=1
else:
o+=1
if o%2 !=0:
print("YES")
continue
elif o==0:
print("NO")
continue
elif e==0 and o>0 and o%2==0:
print("NO")
continue
elif e>0 and o>0 and e%2==0 and o%2==0:
print("YES")
continue
else:
print("NO")
continue
solve()
``` | instruction | 0 | 25,779 | 12 | 51,558 |
No | output | 1 | 25,779 | 12 | 51,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
n=int(input())
for _ in range(n):
n1=int(input())
ll=list(map(int,input().split()))
max=0
for i in range(n1-1):
if abs(ll[i]-ll[i+1])>max:
max=abs(ll[i]-ll[i+1])
if max>1:
print("NO")
else:
print("YES")
``` | instruction | 0 | 25,780 | 12 | 51,560 |
No | output | 1 | 25,780 | 12 | 51,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i β j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| β€ 1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).
Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100), where a_i is the i-th element of a.
Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.
Example
Input
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
Output
YES
YES
NO
NO
YES
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose i=1 and j=3 and remove a_i (so a becomes [2; 2]);
* choose i=1 and j=2 and remove a_j (so a becomes [2]).
In the second test case of the example, we can choose any possible i and j any move and it doesn't matter which element we remove.
In the third test case of the example, there is no way to get rid of 2 and 4.
Submitted Solution:
```
for _ in range(int(input())):
n=input()
l=list(map(int,input().split()))
for i in range(len(l)):
left=0
right=len(l)-1
if abs(l[left]-l[right])<=1:
mi=min(l[left],l[right])
l.remove(mi)
if len(l)==0:
print("YES")
else:
print("NO")
``` | instruction | 0 | 25,781 | 12 | 51,562 |
No | output | 1 | 25,781 | 12 | 51,563 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,798 | 12 | 51,596 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect
#?############################################################
def isPrime(x):
for i in range(2, x):
if i*i > x:
break
if (x % i == 0):
return False
return True
#?############################################################
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den, p - 2, p)) % p
#?############################################################
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n))+1, 2):
while n % i == 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
return list(set(l))
#?############################################################
def power(x, y, p):
res = 1
x = x % p
if (x == 0):
return 0
while (y > 0):
if ((y & 1) == 1):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
#?############################################################
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#?############################################################
def digits(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
#?############################################################
def ceil(n, x):
if (n % x == 0):
return n//x
return n//x+1
#?############################################################
def mapin():
return [int(x) for x in input().split()]
def sapin():
return [int(x) for x in input()]
#?############################################################
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
# python3 15.py<in>op
t = int(input())
for _ in range(t):
n,m = mapin()
d = []
for i in range(n):
temp =sapin()
d.append(temp)
# print(d)
ans = []
for i in range(n-1):
for j in range(m):
if(d[i][j]):
if(j == m-1):
ans.append([i+1, j+1, i+2, j+1, i+2, j])
d[i][j] = 0
d[i+1][j] ^=1
d[i+1][j-1] ^=1
else:
ans.append([i+1, j+1, i+2, j+1, i+2, j+2])
d[i][j] = 0
d[i+1][j] ^=1
d[i+1][j+1] ^=1
i = n-1
for j in range(m):
if(d[-1][j]):
if(j == m-1):
# pass
ans.append([i+1, j+1, i+1, j, i, j+1])
d[i][j] ^=1
d[i][j-1] ^=1
d[i-1][j] ^=1
# print(*d[-1])
ans.append([i+1, j+1, i, j, i, j+1])
d[i][j] ^=1
d[i-1][j-1] ^=1
d[i-1][j] ^=1
ans.append([i+1, j+1, i+1, j, i, j])
d[i][j] ^=1
d[i][j-1] ^=1
d[i-1][j-1] ^=1
else:
ans.append([i+1, j+1, i, j+1, i, j+2])
d[i][j] = 0
d[i-1][j] ^=1
d[i-1][j+1] ^=1
ans.append([i+1, j+2, i, j+1, i, j+2])
d[i][j+1] ^=1
d[i-1][j] ^=1
d[i-1][j+1] ^=1
print(len(ans))
for i in ans:
print(*i)
# for i in d:
# print(*i)
``` | output | 1 | 25,798 | 12 | 51,597 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,799 | 12 | 51,598 |
Tags: constructive algorithms, implementation
Correct Solution:
```
def main():
from sys import stdin
#from math import gcd
from random import randint, choice, shuffle
from functools import lru_cache
input = stdin.readline
#input = open('in', 'r').readline
dx = [0, 0, 1, 1]
dy = [0, 1, 0, 1]
def g(i, j, a):
return (i + dx[a] + 1, j + dy[a] + 1)
def f(i, j, a, b, c):
return (g(i, j, a), g(i, j, b), g(i, j, c))
for _ in range(int(input())):
ans = []
n, m = map(int, input().split())
for i in range(n):
s = input()
for j in range(m):
if s[j] == '0':
continue
if i != n - 1 and j != m - 1:
ans.append(f(i, j, 0, 1, 3))
ans.append(f(i, j, 0, 2, 3))
ans.append(f(i, j, 0, 1, 2))
elif i != n - 1:
ans.append(f(i, j - 1, 0, 1, 2))
ans.append(f(i, j - 1, 1, 2, 3))
ans.append(f(i, j - 1, 0, 1, 3))
elif j != m - 1:
ans.append(f(i - 1, j, 0, 1, 2))
ans.append(f(i - 1, j, 1, 2, 3))
ans.append(f(i - 1, j, 0, 2, 3))
else:
ans.append(f(i - 1, j - 1, 0, 1, 3))
ans.append(f(i - 1, j - 1, 0, 2, 3))
ans.append(f(i - 1, j - 1, 1, 2, 3))
ans.sort()
p = []
c = 0
ans1 = []
for i in ans:
if i == p:
c += 1
else:
if c & 1:
ans1.append(p)
c = 1
p = i
if c & 1:
ans1.append(p)
print(len(ans1))
for i in ans1:
for j in i:
print(*j, end=' ')
print()
main()
``` | output | 1 | 25,799 | 12 | 51,599 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,800 | 12 | 51,600 |
Tags: constructive algorithms, implementation
Correct Solution:
```
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
#from __future__ import print_function, division #while using python2
# from itertools import accumulate
# from collections import defaultdict, Counter
def flip(x, y, n, m):
a = [x+1, y+1]
b = []
c = []
d = []
if y < m-1:
b = [a[0], a[1]+1]
else:
b = [a[0], a[1]-1]
if x < n-1:
c = [a[0]+1, a[1]]
d = [b[0]+1, b[1]]
else:
c = [a[0]-1, a[1]]
d = [b[0]-1, b[1]]
print(*a, *b, *c)
print(*a, *b, *d)
print(*a, *c, *d)
def main():
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
for case in range(int(input())):
n, m = [int(x) for x in input().split()]
grid = []
ct = 0
for i in range(n):
s = input()
arr = [int(x) for x in s]
ct += arr.count(1)
grid.append(arr)
print(ct*3)
for i in range(n):
for j in range(m):
if grid[i][j] == 0:
continue
flip(i, j, n, m)
#------------------ Python 2 and 3 footer by Pajenegod and c1729-----------------------------------------
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
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 | 25,800 | 12 | 51,601 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,801 | 12 | 51,602 |
Tags: constructive algorithms, implementation
Correct Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math
def gift():
for _ in range(t):
n,m = list(map(int,input().split()))
arys = []
for i in range(n):
tem = [int(x) for x in input()]
arys.append(tem)
ans = []
for i in range(n-1):
for j in range(m-1):
x1 = arys[i][j]
x2 = arys[i][j+1]
x3 = arys[i+1][j]
x4 = arys[i+1][j+1]
if j!=m-2:
if sum([x1,x3])==0:
continue
else:
#print(x1,x2,x3,x4)
diu = []
if x1==1:
diu.append(i)
diu.append(j)
if x3==1:
diu.append(i+1)
diu.append(j)
if len(diu)==2:
diu.append(i)
diu.append(j+1)
diu.append(i+1)
diu.append(j+1)
else:
if x2==1:
diu.append(i)
diu.append(j+1)
if len(diu)==4:
diu.append(i+1)
diu.append(j+1)
ans.append(diu)
## if (len(diu)!=6):
## print(sum([x1,x2,x3,x4]))
## print(diu,'ear')
for z in range(3):
x = diu[z*2]
y = diu[z*2+1]
arys[x][y] = (1-arys[x][y])
else:
if sum([x1,x2,x3,x4])==4:
ans.append([i,j+1,i+1,j,i+1,j+1])
ans.append([i,j,i,j+1,i+1,j])
ans.append([i,j,i,j+1,i+1,j+1])
ans.append([i,j,i+1,j,i+1,j+1])
arys[i][j]=0
arys[i][j+1]=0
arys[i+1][j]=0
arys[i+1][j+1]=0
elif sum([x1,x2,x3,x4])==0:
continue
else:
while sum([x1,x2,x3,x4])!=0:
x1 = arys[i][j]
x2 = arys[i][j+1]
x3 = arys[i+1][j]
x4 = arys[i+1][j+1]
diu = []
#print(x1,x2,x3,x4)
if sum([x1,x2,x3,x4])==1:
if x1==1:
diu.extend([i,j])
elif x2==1:
diu.extend([i,j+1])
elif x3==1:
diu.extend([i+1,j])
else:
diu.extend([i+1,j+1])
if x1==0:
diu.extend([i,j])
if x2==0:
diu.extend([i,j+1])
if len(diu)==4:
if x3==0:
diu.extend([i+1,j])
else:
diu.extend([i+1,j+1])
elif len(diu)==2:
diu.extend([i+1,j])
diu.extend([i+1,j+1])
elif sum([x1,x2,x3,x4])==2:
if x1==0:
diu.extend([i,j])
if x2==0:
diu.extend([i,j+1])
if x3==0:
diu.extend([i+1,j])
if x4==0:
diu.extend([i+1,j+1])
if x1==1:
diu.extend([i,j])
elif x2==1:
diu.extend([i,j+1])
elif x3==1:
diu.extend([i+1,j])
else:
diu.extend([i+1,j+1])
elif sum([x1,x2,x3,x4])==3:
if x1==1:
diu.extend([i,j])
if x2==1:
diu.extend([i,j+1])
if x3==1:
diu.extend([i+1,j])
if x4==1:
diu.extend([i+1,j+1])
if (len(diu)>6):
print(sum([x1,x2,x3,x4]))
print(diu,'lat')
if (len(diu)!=0):
ans.append(diu)
for z in range(3):
x = diu[z*2]
y = diu[z*2+1]
arys[x][y] = (1-arys[x][y])
yield len(ans)
for ele in ans:
yield " ".join([str(x+1) for x in ele])
if __name__ == '__main__':
t= int(input())
ans = gift()
print(*ans,sep='\n')
#"{} {} {}".format(maxele,minele,minele)
``` | output | 1 | 25,801 | 12 | 51,603 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,802 | 12 | 51,604 |
Tags: constructive algorithms, implementation
Correct Solution:
```
for _ in range(int(input())):
n,m = list(map(int,input().split()))
mat = []
for i in range(n):
s = list(input())
for i in range(len(s)):
s[i] = int(s[i])
mat.append(s)
ops = []
for i in range(n-1):
for j in range(m-1):
# not the last block
if j!=m-2:
if mat[i][j]==0 and mat[i+1][j]==0:
continue
else:
if mat[i][j]==1 and mat[i+1][j]==1:
ops.append([i+1,j+1,i+2,j+1,i+1,j+2])
mat[i][j] = 0
mat[i+1][j] = 0
mat[i][j+1]^=1
elif mat[i][j]==1:
ops.append([i+1,j+1,i+1,j+2,i+2,j+2])
mat[i][j] = 0
mat[i][j+1]^=1
mat[i+1][j+1]^=1
elif mat[i+1][j]==1:
ops.append([i+2,j+1,i+1,j+2,i+2,j+2])
mat[i+1][j] = 0
mat[i][j+1]^=1
mat[i+1][j+1]^=1
else:
a,b,c,d = mat[i][j],mat[i][j+1],mat[i+1][j],mat[i+1][j+1]
cnt = 0
for k in [a,b,c,d]:
if k==1:
cnt+=1
if cnt==3:
temp = []
if mat[i][j]==1:
temp+=[i+1,j+1]
if mat[i+1][j]==1:
temp+=[i+2,j+1]
if mat[i][j+1]==1:
temp+=[i+1,j+2]
if mat[i+1][j+1]==1:
temp+=[i+2,j+2]
ops.append(temp)
mat[i][j] = mat[i+1][j] = mat[i][j+1] = mat[i+1][j+1] = 0
elif cnt==2:
x,y,z1,z2 = [],[],[],[]
if mat[i][j]==0:
if x==[]:
x = [i+1,j+1]
else:
y = [i+1,j+1]
if mat[i+1][j]==0:
if x==[]:
x = [i+2,j+1]
else:
y = [i+2,j+1]
if mat[i][j+1]==0:
if x==[]:
x = [i+1,j+2]
else:
y = [i+1,j+2]
if mat[i+1][j+1]==0:
if x==[]:
x = [i+2,j+2]
else:
y = [i+2,j+2]
if mat[i][j]==1:
if z1==[]:
z1 = [i+1,j+1]
else:
z2 = [i+1,j+1]
if mat[i+1][j]==1:
if z1==[]:
z1 = [i+2,j+1]
else:
z2 = [i+2,j+1]
if mat[i][j+1]==1:
if z1==[]:
z1 = [i+1,j+2]
else:
z2 = [i+1,j+2]
if mat[i+1][j+1]==1:
if z1==[]:
z1 = [i+2,j+2]
else:
z2 = [i+2,j+2]
mat[i][j] = mat[i+1][j] = mat[i][j+1] = mat[i+1][j+1] = 0
temp = x+y+z1
ops.append(temp)
temp = x+y+z2
ops.append(temp)
# cnt == 1
elif cnt==1:
if mat[i][j]==1:
x = [i+1,j+1]
z1,z2,z3 = [i+2,j+1],[i+1,j+2],[i+2,j+2]
if mat[i+1][j]==1:
x = [i+2,j+1]
z1,z2,z3 = [i+1,j+1],[i+1,j+2],[i+2,j+2]
if mat[i+1][j+1]==1:
x = [i+2,j+2]
z1,z2,z3 = [i+2,j+1],[i+1,j+2],[i+1,j+1]
if mat[i][j+1]==1:
x = [i+1,j+2]
z1,z2,z3 = [i+2,j+1],[i+2,j+2],[i+1,j+1]
temp = z1+z2+x
ops.append(temp)
temp = z1+z3+x
ops.append(temp)
temp = z2+z3+x
ops.append(temp)
mat[i][j] = mat[i+1][j] = mat[i][j+1] = mat[i+1][j+1] = 0
elif cnt==4:
ops.append([i+1,j+1,i+1,j+2,i+2,j+1])
mat[i][j] = 0
mat[i][j+1] = 0
mat[i+1][j] = 0
if mat[i][j]==1:
x = [i+1,j+1]
z1,z2,z3 = [i+2,j+1],[i+1,j+2],[i+2,j+2]
if mat[i+1][j]==1:
x = [i+2,j+1]
z1,z2,z3 = [i+1,j+1],[i+1,j+2],[i+2,j+2]
if mat[i+1][j+1]==1:
x = [i+2,j+2]
z1,z2,z3 = [i+2,j+1],[i+1,j+2],[i+1,j+1]
if mat[i][j+1]==1:
x = [i+1,j+2]
z1,z2,z3 = [i+2,j+1],[i+2,j+2],[i+1,j+1]
temp = z1+z2+x
ops.append(temp)
temp = z1+z3+x
ops.append(temp)
temp = z2+z3+x
ops.append(temp)
mat[i][j] = mat[i+1][j] = mat[i][j+1] = mat[i+1][j+1] = 0
print(len(ops))
for i in ops:
print(*i)
``` | output | 1 | 25,802 | 12 | 51,605 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,803 | 12 | 51,606 |
Tags: constructive algorithms, implementation
Correct Solution:
```
t = int(input())
def put(i, j):
print(i, j, i+1, j, i, j+1)
print(i, j, i+1, j+1, i, j+1)
print(i, j, i+1, j+1, i+1, j)
def put2(i, j):
print(i, j, i-1, j, i, j+1)
print(i, j, i-1, j+1, i, j+1)
print(i, j, i-1, j+1, i-1, j)
def put3(i, j):
print(i, j, i+1, j, i, j-1)
print(i, j, i+1, j-1, i, j-1)
print(i, j, i+1, j-1, i+1, j)
def put4(i, j):
print(i, j, i-1, j, i, j-1)
print(i, j, i-1, j-1, i, j-1)
print(i, j, i-1, j-1, i-1, j)
for _ in range(t):
n, m = map(int, input().split())
mat = []
n1 = 0
for l in range(n):
mat.append(input())
n1 += sum([int(x) for x in mat[l]])
print(3*n1)
for i in range(n-1):
for j in range(m-1):
if mat[i][j] == '1':
put(i+1, j+1)
i = n-1
for j in range(m-1):
if mat[i][j] == '1':
put2(i+1, j+1)
j = m-1
for i in range(n-1):
if mat[i][j] == '1':
put3(i+1, j+1)
if mat[n-1][m-1] == '1':
put4(n, m)
``` | output | 1 | 25,803 | 12 | 51,607 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,804 | 12 | 51,608 |
Tags: constructive algorithms, implementation
Correct Solution:
```
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
res = [None] * n
for i in range(n):
res[i] = II()
return res
def LIR(n):
res = [None] * n
for i in range(n):
res[i] = LI()
return res
def FR(n):
res = [None] * n
for i in range(n):
res[i] = IF()
return res
def LIR(n):
res = [None] * n
for i in range(n):
res[i] = IF()
return res
def LIR_(n):
res = [None] * n
for i in range(n):
res[i] = LI_()
return res
def SR(n):
res = [None] * n
for i in range(n):
res[i] = S()
return res
def LSR(n):
res = [None] * n
for i in range(n):
res[i] = LS()
return res
mod = 1000000007
inf = float('INF')
#solve
def solve():
t = II()
kuso = {"1":"0", "0":"1"}
f = {1:3,2:2,3:1,4:4}
def fff(g,a):
one = a.count("1")
if one == 0:
return
tmp = []
if one == 1 or one == 3:
for i in range(4):
if a[i] == "1":
tmp.append(i)
for i in range(4):
if len(tmp) == 3:
break
if a[i] == "0":
tmp.append(i)
for i in range(3):
a[tmp[i]] = kuso[a[tmp[i]]]
tmp[i] = " ".join(map(str, g[tmp[i]]))
ans.append(" ".join(tmp))
fff(g,a)
return
else:
for i in range(4):
if a[i] == "0":
tmp.append(i)
for i in range(4):
if len(tmp) == 3:
break
if a[i] == "1":
tmp.append(i)
for i in range(3):
a[tmp[i]] = kuso[a[tmp[i]]]
tmp[i] = " ".join(map(str, g[tmp[i]]))
ans.append(" ".join(tmp))
fff(g,a)
return
for i in range(t):
n,m = LI()
s = [list(input()) for i in range(n)]
ans = []
for y in range(0,n-1,2):
for x in range(0,m-1,2):
a0 = s[y][x]
a1 = s[y][x+1]
a2 = s[y+1][x]
a3 = s[y+1][x+1]
s[y][x] = "0"
s[y+1][x] = "0"
s[y][x+1] = "0"
s[y+1][x+1] = "0"
g = [[y+1,x+1], [y+1,x+2],[y+2,x+1],[y+2,x+2]]
a = [a0, a1, a2, a3]
c = a.count("1")
if c == 0:
continue
fff(g,a)
x = m - 2
a0 = s[y][x]
a1 = s[y][x+1]
a2 = s[y+1][x]
a3 = s[y+1][x+1]
s[y][x] = "0"
s[y+1][x] = "0"
s[y][x+1] = "0"
s[y+1][x+1] = "0"
g = [[y+1,x+1], [y+1,x+2],[y+2,x+1],[y+2,x+2]]
a = [a0, a1, a2, a3]
c = a.count("1")
if c == 0:
continue
fff(g,a)
y = n - 2
for x in range(0,m-1,2):
a0 = s[y][x]
a1 = s[y][x+1]
a2 = s[y+1][x]
a3 = s[y+1][x+1]
s[y][x] = "0"
s[y+1][x] = "0"
s[y][x+1] = "0"
s[y+1][x+1] = "0"
g = [[y+1,x+1], [y+1,x+2],[y+2,x+1],[y+2,x+2]]
a = [a0, a1, a2, a3]
c = a.count("1")
if c == 0:
continue
fff(g,a)
x = m - 2
a0 = s[y][x]
a1 = s[y][x+1]
a2 = s[y+1][x]
a3 = s[y+1][x+1]
s[y][x] = "0"
s[y+1][x] = "0"
s[y][x+1] = "0"
s[y+1][x+1] = "0"
g = [[y+1,x+1], [y+1,x+2],[y+2,x+1],[y+2,x+2]]
a = [a0, a1, a2, a3]
c = a.count("1")
if c != 0:
fff(g,a)
print(len(ans))
for i in ans:
print(i)
return
#main
if __name__ == '__main__':
solve()
``` | output | 1 | 25,804 | 12 | 51,609 |
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 number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.
You are given a binary table of size n Γ m. This table consists of symbols 0 and 1.
You can make such operation: select 3 different cells that belong to one 2 Γ 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0).
Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations.
It can be proved that it is always possible.
Input
The first line contains a single integer t (1 β€ t β€ 5000) β the number of test cases. The next lines contain descriptions of test cases.
The first line of the description of each test case contains two integers n, m (2 β€ n, m β€ 100).
Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table.
It is guaranteed that the sum of nm for all test cases does not exceed 20000.
Output
For each test case print the integer k (0 β€ k β€ 3nm) β the number of operations.
In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 β€ x_1, x_2, x_3 β€ n, 1 β€ y_1, y_2, y_3 β€ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ 2 square.
Example
Input
5
2 2
10
11
3 3
011
101
110
4 4
1111
0110
0110
1111
5 5
01011
11001
00010
11011
10000
2 3
011
101
Output
1
1 1 2 1 2 2
2
2 1 3 1 3 2
1 2 1 3 2 3
4
1 1 1 2 2 2
1 3 1 4 2 3
3 2 4 1 4 2
3 3 4 3 4 4
4
1 2 2 1 2 2
1 4 1 5 2 5
4 1 4 2 5 1
4 4 4 5 3 4
2
1 3 2 2 2 3
1 2 2 1 2 2
Note
In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0.
In the second test case:
* operation with cells (2, 1), (3, 1), (3, 2). After it the table will be:
011
001
000
* operation with cells (1, 2), (1, 3), (2, 3). After it the table will be:
000
000
000
In the fifth test case:
* operation with cells (1, 3), (2, 2), (2, 3). After it the table will be:
010
110
* operation with cells (1, 2), (2, 1), (2, 2). After it the table will be:
000
000
| instruction | 0 | 25,805 | 12 | 51,610 |
Tags: constructive algorithms, implementation
Correct Solution:
```
for _ in range(int(input())):
n,m=map(int,input().split(" "))
a=[list(map(int,list(input()))) for x in range(n)]
ans=[]
def getone(arr):
for xx in range(2):
for yy in range(2):
if arr[xx][yy]==1:
return xx,yy
def getzero(arr):
for xx in range(2):
for yy in range(2):
if arr[xx][yy]==0:
return xx,yy
for x in range(n):
for y in range(m):
if x+1<n and y+1<m:
temp=list([a[x][y:y+2],a[x+1][y:y+2]])
sm=0
for xxx in temp:
sm=sm+sum(xxx)
if sm==0:
continue
if sm==1:
xx,yy=getone(temp)
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
tempx=xx
xx=(xx+1)%2
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
yy=(yy+1)%2
xx=(xx+1)%2
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
a[x][y]=0
a[x+1][y]=0
a[x][y+1]=0
a[x+1][y+1]=0
elif sm==3:
for xx in range(2):
for yy in range(2):
if temp[xx][yy]==1:
ans.append([x+1+xx,y+1+yy])
a[x][y]=0
a[x+1][y]=0
a[x][y+1]=0
a[x+1][y+1]=0
elif sm==4:
for xx in range(2):
for yy in range(2):
if temp[xx][yy]==1:
ans.append([x+1+xx,y+1+yy])
ans.pop()
xx,yy=1,1
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
tempx=xx
xx=(xx+1)%2
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
yy=(yy+1)%2
xx=(xx+1)%2
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
a[x][y]=0
a[x+1][y]=0
a[x][y+1]=0
a[x+1][y+1]=0
elif sm==2:
for xx in range(2):
for yy in range(2):
if temp[xx][yy]==1:
ans.append([x+1+xx,y+1+yy])
xx,yy=getzero(temp)
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
tempx=xx
xx=(xx+1)%2
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
yy=(yy+1)%2
xx=(xx+1)%2
ans.append([x+1+xx,y+1+yy])
ans.append([x+1+(xx+1)%2,y+1+yy])
ans.append([x+1+xx,y+1+(yy+1)%2])
a[x][y]=0
a[x+1][y]=0
a[x][y+1]=0
a[x+1][y+1]=0
print(len(ans)//3)
cnt=0
for x in range(len(ans)):
print(*ans[x],end=" ")
cnt+=1
if cnt==3:
print()
cnt=0
``` | output | 1 | 25,805 | 12 | 51,611 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,842 | 12 | 51,684 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
# from random import randint
from bisect import bisect_left as bl
def main():
n,q=readIntArr()
a=readIntArr()
idxes=[[] for _ in range(n+1)]
for i,x in enumerate(a):
idxes[x].append(i)
def getCnts(l,r,elem):
le=bl(idxes[elem],l)
ri=bl(idxes[elem],r)
cnts=ri-le
if ri<len(idxes[elem]) and idxes[elem][ri]==r:
cnts+=1
return cnts
rand=[3986840, 9105399, 9295665, 5185430, 9481084, 6286642, 4509520, 8680737, 7663262, 3911483, 2905903, 8829326, 3632860, 3556701, 1943314, 4147565, 6945365, 9156375, 8569604, 3518362, 9247224, 9907745, 8041336] #, 5294215, 6148921, 7892242, 2008293, 7240960, 3406413, 1685961]
rand=rand[:20]
allans=[]
for _ in range(q):
l,r=readIntArr()
l-=1
r-=1
total=r-l+1
ans=1
for ran in rand:
randomIdx=l+ran%(r-l+1)
cnts=getCnts(l,r,a[randomIdx])
diff=total-cnts
ans2=1+total-(diff*2+1)
if ans2>ans:
ans=ans2
break
allans.append(ans)
multiLineArrayPrint(allans)
return
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
# input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultVal,dimensionArr): # eg. makeArr(0,[n,m])
dv=defaultVal;da=dimensionArr
if len(da)==1:return [dv for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(x,y):
print('? {} {}'.format(x,y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print('! {}'.format(ans))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
# MOD=998244353
for _abc in range(1):
main()
``` | output | 1 | 25,842 | 12 | 51,685 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,843 | 12 | 51,686 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
#lfrom hxu10 (113540436)
import random
import bisect
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n,q = map(int,input().split())
arr = list(map(int,input().split()))
freindex = [[] for i in range(n+1)]
for i in range(n):
freindex[arr[i]].append(i)
res = []
for i in range(q):
l,r = map(int,input().split())
l -= 1
r -= 1
sublength = r - l + 1
ans = 1
s = set()
for j in range(25):
randindex = int(random.random()*(sublength))+l
num = arr[randindex]
if num in s: continue
s.add(num)
if len(freindex[num])<=(sublength+1)//2: continue
loc1 = bisect.bisect_left(freindex[num],l)
loc2 = bisect.bisect(freindex[num],r)
subfre = loc2 - loc1
if subfre>(sublength+1)//2:
ans = subfre*2-sublength
break
res += [str(ans)]
print("\n".join(res))
``` | output | 1 | 25,843 | 12 | 51,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,844 | 12 | 51,688 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
import random,bisect,io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n,q = map(int,input().split());arr = list(map(int,input().split()));freindex = [[] for i in range(n+1)]
for i in range(n):freindex[arr[i]].append(i)
for i in range(q):
l,r = map(int,input().split())
l -= 1
r -= 1
sublength = r - l + 1
ans = 1
s = set()
for j in range(25):
randindex = int(random.random()*(sublength))+l
num = arr[randindex]
if num in s: continue
s.add(num)
if len(freindex[num])<=(sublength+1)//2: continue
loc1 = bisect.bisect_left(freindex[num],l)
loc2 = bisect.bisect(freindex[num],r)
subfre = loc2 - loc1
if subfre>(sublength+1)//2:
ans = subfre*2-sublength
break
print(ans)
``` | output | 1 | 25,844 | 12 | 51,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,845 | 12 | 51,690 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
#import random
#import bisect
import io,os
raw_input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def search(arr,target):
front = 0
rear = len(arr)
while front<rear:
mid = (front+rear)>>1
if arr[mid]<target:
front = mid + 1
else:
rear = mid
return front
#print(search([1,3,3,4,5],1))
n,q = map(int,raw_input().split())
arr = list(map(int,raw_input().split()))
freindex = [[] for i in range(n+1)]
for i in range(n):
freindex[arr[i]].append(i)
#print(freindex)
seed = 1926
for i in range(q):
l,r = map(int,raw_input().split())
l -= 1
r -= 1
sublength = r - l + 1
flag = False
dic = {}
for j in range(26):
seed = (seed*16807)%2147483647
randindex = seed%sublength+l
num = arr[randindex]
if num in dic: continue
dic[num] = 1
if len(freindex[num])<=(sublength+1)//2: continue
loc1 = search(freindex[num],l)
loc2 = search(freindex[num],r+1)
# loc1 = bisect.bisect_left(freindex[num],l)
# loc2 = bisect.bisect(freindex[num],r)
subfre = loc2 - loc1
# print(randindex,num,subfre)
if subfre>(sublength+1)//2:
flag = True
ans = subfre*2-sublength
break
if flag:
print(ans)
else:
print(1)
``` | output | 1 | 25,845 | 12 | 51,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,846 | 12 | 51,692 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
#from hxu10 (113540436)
import random
import bisect
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n,q = map(int,input().split())
arr = list(map(int,input().split()))
freindex = [[] for i in range(n+1)]
for i in range(n):
freindex[arr[i]].append(i)
for i in range(q):
l,r = map(int,input().split())
l -= 1
r -= 1
sublength = r - l + 1
ans = 1
s = set()
for j in range(25):
randindex = int(random.random()*(sublength))+l
num = arr[randindex]
if num in s: continue
s.add(num)
if len(freindex[num])<=(sublength+1)//2: continue
loc1 = bisect.bisect_left(freindex[num],l)
loc2 = bisect.bisect(freindex[num],r)
subfre = loc2 - loc1
if subfre>(sublength+1)//2:
ans = subfre*2-sublength
break
print(ans)
``` | output | 1 | 25,846 | 12 | 51,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,847 | 12 | 51,694 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
import sys,os,io
import bisect
from random import random
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n,q = [int(i) for i in input().split()]
a = [int(i)-1 for i in input().split()]
ind = [[] for i in range (n)]
for i in range (len(a)):
ind[a[i]].append(i)
for i in range (q):
flag = 0
l,r = [int(i)-1 for i in input().split()]
rem = r-l+1
total = rem
total2 = (total+1)//2
vis = set()
for j in range (25):
rand = int(random()*(total)) + l
ele = a[rand]
if ele in vis:
continue
vis.add(ele)
if len(ind[ele])<= total2:
continue
right = bisect.bisect(ind[ele], r)
left = bisect.bisect_left(ind[ele],l)
cnt = right - left
if cnt>total2:
flag = cnt
break
rem -= cnt
if rem<=total2:
break
if flag:
print(2*flag - total)
else:
print(1)
``` | output | 1 | 25,847 | 12 | 51,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,848 | 12 | 51,696 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
#Fast I/O
import sys,os
import math
# To enable the file I/O i the below 2 lines are uncommented.
# read from in.txt if uncommented
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
# will print on Console if file I/O is not activated
#if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
# inputs template
from io import BytesIO, IOBase
def main():
import random,bisect
n,q=MI()
arr=list(MI())
count=[list() for i in range(n+1)]
for i in range(n):
count[arr[i]].append(i)
for _ in range(q):
l,r=MI()
l-=1
r-=1
m=(r-l+1)
out=1
s=set()
for i in range(22):
x=int(random.random() * (r - l + 1)) + l
if arr[x] in s:
continue
s.add(arr[x])
if len(count[arr[x]])<=(m+1)//2:
continue
ir=bisect.bisect_right(count[arr[x]],r)
il=bisect.bisect_left(count[arr[x]],l)
if ir-il>(m+1)//2:
out=2*(ir-il)-m
break
outP(out)
# Sample Inputs/Output
# 6 2
# 1 3 2 3 3 2
# 1 6
# 2 5
# 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")
#for array of integers
def MI():return (map(int,input().split()))
# endregion
#for fast output, always take string
def outP(var): sys.stdout.write(str(var)+'\n')
# end of any user-defined functions
MOD=10**9+7
# main functions for execution of the program.
if __name__ == '__main__':
#This doesn't works here but works wonders when submitted on CodeChef or CodeForces
main()
``` | output | 1 | 25,848 | 12 | 51,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3]. | instruction | 0 | 25,849 | 12 | 51,698 |
Tags: binary search, data structures, greedy, implementation, sortings
Correct Solution:
```
import random
#import bisect
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def search(arr,target):
front = 0
rear = len(arr)
while front<rear:
mid = (front+rear)>>1
if arr[mid]<target:
front = mid + 1
else:
rear = mid
return front
#print(search([1,3,3,4,5],1))
n,q = map(int,input().split())
arr = list(map(int,input().split()))
freindex = [[] for i in range(n+1)]
for i in range(n):
freindex[arr[i]].append(i)
#print(freindex)
for i in range(q):
l,r = map(int,input().split())
l -= 1
r -= 1
sublength = r - l + 1
flag = False
dic = {}
for j in range(25):
randindex = int(random.random()*(sublength))+l
num = arr[randindex]
if num in dic: continue
dic[num] = 1
if len(freindex[num])<=(sublength+1)//2: continue
loc1 = search(freindex[num],l)
loc2 = search(freindex[num],r+1)
# loc1 = bisect.bisect_left(freindex[num],l)
# loc2 = bisect.bisect(freindex[num],r)
subfre = loc2 - loc1
# print(randindex,num,subfre)
if subfre>(sublength+1)//2:
flag = True
ans = subfre*2-sublength
break
if flag:
print(ans)
else:
print(1)
``` | output | 1 | 25,849 | 12 | 51,699 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
# coding: utf-8
from bisect import bisect, bisect_left
from random import random
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n, q = map(int, input().split())
a = list(map(int, input().split()))
v = [list() for i in range(n + 1)]
for i in range(n):
v[a[i]].append(i)
for i in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
ans = 1
s = set()
for j in range(25):
idx = int(random() * (r - l + 1)) + l
if a[idx] in s:
continue
s.add(a[idx])
if len(v[a[idx]]) <= (r - l + 2) // 2:
continue
left = bisect_left(v[a[idx]], l)
right = bisect(v[a[idx]], r)
if right - left > (r - l + 2) // 2:
ans = 2 * (right - left) - (r - l + 1)
break
print(ans)
``` | instruction | 0 | 25,850 | 12 | 51,700 |
Yes | output | 1 | 25,850 | 12 | 51,701 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
from bisect import bisect,bisect_left
'''
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().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def A(n):return [0]*n
def AI(n,x): return [x]*n
def A2(n,m): return [[0]*m for i in range(n)]
def G(n): return [[] for i in range(n)]
def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)]
#------------------------------------------------------------------------
from random import random
t=1
for i in range(t):
n,q=RL()
a=RLL()
g=[[] for i in range(n+1)]
for i in range(n):
g[a[i]].append(i)
res=[]
for i in range(q):
l,r=RL()
l-=1
r-=1
c=0
s=set()
h=(r-l+2)//2
lt=r-l+1
ans=1
for i in range(20):
x=a[int(random()*(lt))+l]
if x in s:
continue
s.add(x)
lind=bisect_left(g[x],l)
if lind+h<len(g[x]) and g[x][lind+h]<=r:
rind=bisect(g[x],r)-1
ans=(rind-lind+1)*2-lt
break
res.append(str(ans))
print("\n".join(res))
``` | instruction | 0 | 25,851 | 12 | 51,702 |
Yes | output | 1 | 25,851 | 12 | 51,703 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from bisect import bisect_left, bisect_right
from random import randrange
RAND = [randrange(10**9) for i in range(23)]
n, q = map(int, input().split())
arr = list(map(int, input().split()))
d = [[] for i in range(n+1)]
for i, a in enumerate(arr):
d[a].append(i)
def solve():
le, ri = map(int, input().split())
le -= 1
ri -= 1
size = ri - le + 1
s = set()
for r in RAND:
x = arr[r%size+le]
if x in s:
continue
s.add(x)
i = bisect_left(d[x], le)
j = bisect_right(d[x], ri)
if (j-i)<<1 > size:
print(((j-i)<<1) - size)
return
print(1)
for i in range(q):
solve()
``` | instruction | 0 | 25,852 | 12 | 51,704 |
Yes | output | 1 | 25,852 | 12 | 51,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
import sys,os,io,random
import math,bisect,operator
inf,mod = float('inf'),10**9+7
# sys.setrecursionlimit(10 ** 6)
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
Neo = lambda : list(map(int,input().split()))
from bisect import bisect_left
# test, = Neo()
class SegmentTree():
def __init__(self, n, oper, e):
self.n = n
self.oper = oper
self.e = e
self.log = (n - 1).bit_length()
# print(self.log)
self.size = 1 << self.log
self.data = [e] * (2 * self.size)
def update(self, k):
self.data[k] = self.oper(self.data[2 * k], self.data[2 * k + 1])
def build(self, arr):
for i in range(self.n):
self.data[self.size + i] = arr[i]
for i in range(self.size - 1, 0, -1):
self.update(i)
def set(self, p, x):
p += self.size
self.data[p] = x
for i in range(self.log):
p >>= 1
self.update(p)
def get(self, p):
return self.data[p + self.size]
def prod(self, l, r):
sml = smr = self.e
l += self.size
r += self.size
while l < r:
if l & 1:
sml = self.oper(sml, self.data[l])
l += 1
if r & 1:
r -= 1
smr = self.oper(self.data[r], smr)
l >>= 1
r >>= 1
return self.oper(sml, smr)
n,q = Neo()
arr = Neo()
d = [[] for i in range(n+1)]
for i, a in enumerate(arr):
d[a].append(i)
e = (-1, 0)
def op(a, b):
if a[0] == b[0]:
return a[0], a[1] + b[1]
elif a[1] > b[1]:
return a[0], a[1] - b[1]
elif a[1] < b[1]:
return b[0], b[1] - a[1]
else:
return -1, 0
seg = list(zip(arr, [1]*n))
# print(seg)
st = SegmentTree(n, op, e)
st.build(seg)
for _ in range(q):
le, ri = map(int, input().split())
le -= 1
v, c = st.prod(le, ri)
if c == 0:
print(1)
else:
m = bisect_left(d[v], ri) - bisect_left(d[v], le)
print(max((m<<1)+le-ri, 1))
``` | instruction | 0 | 25,853 | 12 | 51,706 |
Yes | output | 1 | 25,853 | 12 | 51,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
from random import randint
from bisect import bisect_left as bl
def main():
n,q=readIntArr()
a=readIntArr()
idxes=[[] for _ in range(n+1)]
for i,x in enumerate(a):
idxes[x].append(i)
def getCnts(l,r,elem):
le=bl(idxes[elem],l)
ri=bl(idxes[elem],r)
cnts=ri-le
if ri<len(idxes[elem]) and idxes[elem][ri]==r:
cnts+=1
return cnts
rand=[]
for _ in range(20):
rand.append(randint(1000000,9999999))
allans=[]
for _ in range(q):
l,r=readIntArr()
l-=1
r-=1
total=r-l+1
ans=1
for ran in rand:
randomIdx=l+ran%(r-l+1)
cnts=getCnts(l,r,a[randomIdx])
diff=total-cnts
ans2=1+total-(diff*2+1)
if ans2>ans:
ans=ans2
break
allans.append(ans)
multiLineArrayPrint(allans)
return
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
# input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultVal,dimensionArr): # eg. makeArr(0,[n,m])
dv=defaultVal;da=dimensionArr
if len(da)==1:return [dv for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(x,y):
print('? {} {}'.format(x,y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print('! {}'.format(ans))
sys.stdout.flush()
inf=float('inf')
MOD=10**9+7
# MOD=998244353
for _abc in range(1):
main()
``` | instruction | 0 | 25,854 | 12 | 51,708 |
No | output | 1 | 25,854 | 12 | 51,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
import sys
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()
import random
from bisect import*
from collections import deque,defaultdict,Counter
def I():return int(input())
def LI():return [int(i) for i in input().split()]
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
n,q=LI()
a=LI()
d=defaultdict(list)
for i in range(n):
x=a[i]
d[x]+=i,
def lem(m,a):
if m<=(a+m+1)//2:
return 1
else:
return m-a-1 +1
def sol(l,r):
n=r-l+1
mx=0
s=set()
for i in range(25):
x=RI(l,r)
s.add(a[x])
for x in s:
L=bisect_left(d[x],l)
R=bisect(d[x],r+1)
mx=max(R-L,mx)
#show(x,(l,r),(L,R),s,n,mx,d[x])
return lem(mx,n-mx)
for i in range(q):
l,r=LI()
l-=1;r-=1
ans=sol(l,r)
print(ans)
``` | instruction | 0 | 25,855 | 12 | 51,710 |
No | output | 1 | 25,855 | 12 | 51,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
from bisect import bisect,bisect_left
#------------------------------------------------------------------------
import io,os
import sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
#------------------------------------------------------------------------
def RL(): return map(int,input().split())
def RLL(): return list(map(int,input().split()))
def N(): return int(input())
def A(n):return [0]*n
def AI(n,x): return [x]*n
def A2(n,m): return [[0]*m for i in range(n)]
def G(n): return [[] for i in range(n)]
def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)]
#------------------------------------------------------------------------
from random import random
t=1
for i in range(t):
n,q=RL()
a=RLL()
g=G(n+1)
for i in range(n):
g[a[i]].append(i)
for i in range(q):
l,r=RL()
l-=1
r-=1
c=0
s=set()
h=(r-l+2)//2
lt=r-l+1
ans=1
for i in range(20):
x=a[int(random()*(lt))+l]
if x in s:
continue
s.add(x)
lind=bisect_left(g[x],l)
if lind+h<len(g[x]) and g[x][lind+h]<=r:
rind=bisect(g[x],r)-1
ans=(rind-lind+1)*2-lt
break
print(ans)
``` | instruction | 0 | 25,856 | 12 | 51,712 |
No | output | 1 | 25,856 | 12 | 51,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it:
* pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range;
* stick some of the elements together in the same order they were in the array;
* end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece.
More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than β x/2 β times in it.
He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
Input
The first line contains two integers n and q (1 β€ n,q β€ 3 β
10^5) β the length of the array a and the number of queries.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_i β€ n) β the elements of the array a.
Each of the next q lines contains two integers l and r (1 β€ l β€ r β€ n) β the range of this query.
Output
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
Example
Input
6 2
1 3 2 3 3 2
1 6
2 5
Output
1
2
Note
In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it.
In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
Submitted Solution:
```
#region Header
#!/usr/bin/env python3
# from typing import *
import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq
def input():
return sys.stdin.readline()[:-1]
# sys.setrecursionlimit(1000000)
#endregion
# _INPUT = """6 2
# 1 3 2 3 3 2
# 1 6
# 2 5
# """
# sys.stdin = io.StringIO(_INPUT)
def main():
N, Q = map(int, input().split())
A = list(map(int, input().split()))
max_A = max(A)
N_bucket = int(math.sqrt(N)) + 1
L_bucket = N_bucket ** 2
A = A + [-1] * (L_bucket - N)
freq = [0] * N_bucket
for i in range(N_bucket):
freq_1 = [0] * (max_A+1)
max_value = 0
for k in range(N_bucket*i, N_bucket*(i+1)):
freq_1[A[k]] += 1
if A[k] >= 1:
max_value = max(max_value, freq_1[A[k]])
freq[i] = max_value
occur = [list() for _ in range(max_A+1)]
for i in range(N):
occur[A[i]].append(i)
for _ in range(Q):
L, R = map(int, input().split())
L -= 1
R -= 1
x = math.floor((R-L+1) / 2)
i1 = math.ceil(L / N_bucket)
i2 = math.floor(R / N_bucket)
ans = 0
for i in range(i1, i2+1):
ans = max(ans, math.ceil(freq[i] / x))
j2 = bisect.bisect_left(occur[A[k]], R)
for k in range(L, N_bucket*i1):
j1 = bisect.bisect_left(occur[A[k]], k)
n = j2 - j1 + 1
ans = max(ans, math.ceil(n / x))
for k in range(N_bucket*(i2+1)-1, R+1):
j1 = bisect.bisect_left(occur[A[k]], k)
n = j2 - j1 + 1
ans = max(ans, math.ceil(n / x))
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 25,857 | 12 | 51,714 |
No | output | 1 | 25,857 | 12 | 51,715 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.
Petya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a1, a2, ..., an and b1, b2, ..., bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci = ((ai - 1 + bi - 1) mod n) + 1 (1 β€ i β€ n).
Operation <image> means taking the remainder after dividing number x by number y.
Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x β y) and the pair of permutations y, x are considered distinct pairs.
As the answer can be rather large, print the remainder after dividing it by 1000000007 (109 + 7).
Input
The single line contains integer n (1 β€ n β€ 16).
Output
In the single line print a single non-negative integer β the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7).
Examples
Input
3
Output
18
Input
5
Output
1800 | instruction | 0 | 25,914 | 12 | 51,828 |
Tags: bitmasks, combinatorics, dp, implementation, meet-in-the-middle
Correct Solution:
```
import math
ans = [1, 3, 15, 133, 2025, 37851, 1030367, 36362925]
n = int(input())
if (n % 2 == 1):
print(ans[n // 2] * math.factorial(n) % 1000000007)
else:
print(0)
``` | output | 1 | 25,914 | 12 | 51,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.
Petya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a1, a2, ..., an and b1, b2, ..., bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci = ((ai - 1 + bi - 1) mod n) + 1 (1 β€ i β€ n).
Operation <image> means taking the remainder after dividing number x by number y.
Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x β y) and the pair of permutations y, x are considered distinct pairs.
As the answer can be rather large, print the remainder after dividing it by 1000000007 (109 + 7).
Input
The single line contains integer n (1 β€ n β€ 16).
Output
In the single line print a single non-negative integer β the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7).
Examples
Input
3
Output
18
Input
5
Output
1800 | instruction | 0 | 25,915 | 12 | 51,830 |
Tags: bitmasks, combinatorics, dp, implementation, meet-in-the-middle
Correct Solution:
```
n = int(input())
ans = [1, 3, 5, 7, 9, 11, 13, 15]
dct = \
{
1 : 1,
3 : 18,
5 : 1800,
7 : 670320,
9 : 734832000,
11 : 890786230,
13 : 695720788,
15 : 150347555
}
if n in ans:
print(dct[n])
else:
print(0)
``` | output | 1 | 25,915 | 12 | 51,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.
Petya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a1, a2, ..., an and b1, b2, ..., bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci = ((ai - 1 + bi - 1) mod n) + 1 (1 β€ i β€ n).
Operation <image> means taking the remainder after dividing number x by number y.
Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x β y) and the pair of permutations y, x are considered distinct pairs.
As the answer can be rather large, print the remainder after dividing it by 1000000007 (109 + 7).
Input
The single line contains integer n (1 β€ n β€ 16).
Output
In the single line print a single non-negative integer β the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7).
Examples
Input
3
Output
18
Input
5
Output
1800
Submitted Solution:
```
n = int(input())
ans = [1, 3, 5, 7, 13, 15]
dct = \
{
1 : 1,
3 : 18,
5 : 1800,
7 : 670320,
13 : 695720788,
15 : 150347555
}
if n in ans:
print(dct[n])
else:
print(0)
``` | instruction | 0 | 25,916 | 12 | 51,832 |
No | output | 1 | 25,916 | 12 | 51,833 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.