message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0 | instruction | 0 | 91,762 | 12 | 183,524 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n = int(input())
a = [int(_) for _ in input().split()]
freq = {}
for item in a:
if item in freq:
freq[item] += 1
else:
freq[item] = 1
most_occ = max(freq, key=lambda x: freq[x])
index_center = a.index(most_occ)
print(len(a) - a.count(most_occ))
for i in range(index_center, 0, -1):
if a[i - 1] < most_occ:
a[i - 1] = most_occ
print(1, i, i + 1)
elif a[i - 1] > most_occ:
a[i - 1] = most_occ
print(2, i, i + 1)
for i in range(index_center, n - 1):
if a[i + 1] > most_occ:
a[i + 1] = most_occ
print(2, i + 2, i + 1)
elif a[i + 1] < most_occ:
a[i + 1] = most_occ
print(1, i + 2, i + 1)
``` | output | 1 | 91,762 | 12 | 183,525 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0 | instruction | 0 | 91,763 | 12 | 183,526 |
Tags: constructive algorithms, greedy
Correct Solution:
```
N = int(input())
A = [int(a) for a in input().split()]
B = [0] * (2*10**5+10)
for a in A:
B[a] += 1
ma = 0
mai = 0
for i in range(len(B)):
if B[i] > ma:
ma = B[i]
maa = i
for i in range(N):
if A[i] == maa:
mai = i
break
print(N - ma)
for i in range(mai)[::-1]:
if A[i] < maa:
print(1, i+1, i+2)
elif A[i] > maa:
print(2, i+1, i+2)
for i in range(mai+1, N):
if A[i] < maa:
print(1, i+1, i)
elif A[i] > maa:
print(2, i+1, i)
``` | output | 1 | 91,763 | 12 | 183,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0 | instruction | 0 | 91,764 | 12 | 183,528 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import collections
n = int(input())
a = input().split(" ")
count = collections.Counter()
at = []
for el in a:
count[el] += 1
at.append(int(el))
a = at
k = max(count, key=count.get)
tk = int(k)
print(n - count[k])
pos = 0
con = 0
tcon = 0
arcon = 0
while pos < n:
if a[pos] == tk:
arcon += 1
while con > tcon:
if a[con-1] > a[con]:
print(2,con-1 + 1, con+ 1)
else:
print(1,con-1 + 1, con + 1)
con -= 1
a[con] = a[pos]
tcon = pos + 1
con = pos
if arcon == count[k]:
while pos < n -1:
if a[pos + 1] > a[pos]:
print(2,pos + 2, pos + 1)
else:
print(1,pos + 2, pos + 1)
a[pos + 1] = a[pos]
pos += 1
con += 1
pos += 1
``` | output | 1 | 91,764 | 12 | 183,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0 | instruction | 0 | 91,765 | 12 | 183,530 |
Tags: constructive algorithms, greedy
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
from collections import Counter
c=Counter(l)
a=0
for x,y in c.most_common(1):
a=x
l1=[]
for i in range(n):
if l[i]==a:
l1.append(i)
l2=[]
if l1[0]!=0:
for i in range(l1[0]-1,-1,-1):
if l[i]<a:
l2.append([1,i+1,i+2])
else:
l2.append([2,i+1,i+2])
if l1[-1]!=n-1:
l1.append(n)
for i in range(len(l1)-1):
for j in range(l1[i]+1,l1[i+1]):
if l[j]<a:
l2.append([1,j+1,j])
else:
l2.append([2,j+1,j])
print(len(l2))
for i in l2:
print(*i)
``` | output | 1 | 91,765 | 12 | 183,531 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0 | instruction | 0 | 91,766 | 12 | 183,532 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import collections
def solve():
N=int(input())
A=list(map(int,input().split()))
c=collections.Counter(A)
max_count = sorted(c.values(), reverse=True)[0]
max_key = [k for k in c.keys() if c[k] == max_count][0]
pivot = A.index(max_key)
ans=[]
for i in range(pivot-1, -1, -1):
if A[i]<max_key:
ans.append([1,i+1,i+2])
else:
ans.append([2,i+1,i+2])
#print(max_key,pivot)
for i in range(pivot+1, N):
if A[i]==max_key:
continue
if A[i]<max_key:
ans.append([1,i+1,i])
else:
ans.append([2,i+1,i])
print(len(ans))
for a in ans:
print(' '.join(list(map(str,a))))
solve()
``` | output | 1 | 91,766 | 12 | 183,533 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0 | instruction | 0 | 91,767 | 12 | 183,534 |
Tags: constructive algorithms, greedy
Correct Solution:
```
import string, copy
def solution(n, nums):
counts = {}
for i in nums:
if i not in counts:
counts[i] = 0
counts[i] += 1
tmp = [(k, v) for k, v in counts.items()]
tmp.sort(key=lambda x:x[1])
v = tmp[-1][0]
steps = len(nums) - nums.count(v)
print(steps)
start = nums.index(v)
for i in range(start+1, len(nums)):
if nums[i] == v:
continue
elif nums[i] > v:
print('%d %d %d' % (2, i+1, i))
elif nums[i] < v:
print('%d %d %d' % (1, i+1, i))
for i in range(start-1, -1, -1):
if nums[i] == v:
continue
elif nums[i] > v:
print('%d %d %d' % (2, i+1, i+2))
elif nums[i] < v:
print('%d %d %d' % (1, i+1, i+2))
def parser():
n = int(input())
nums = [int(s) for s in input().split(' ')]
return n, nums
def main():
args = parser()
solution(*args)
if __name__ == '__main__':
main()
``` | output | 1 | 91,767 | 12 | 183,535 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0 | instruction | 0 | 91,768 | 12 | 183,536 |
Tags: constructive algorithms, greedy
Correct Solution:
```
from collections import Counter
def solve():
n = int(input())
arr = list(map(int, input().split(" ")))
highest_freq_items = Counter(arr).most_common(1)[0]
highest_freq = highest_freq_items[1]
highest_freq_num = highest_freq_items[0]
if (n - highest_freq) == 0:
print(0)
return
print(n - highest_freq)
index = arr.index(highest_freq_num)
# Left Side
for i in range(index - 1, -1, -1):
if arr[i] == highest_freq_num:
continue
if arr[i] > highest_freq_num:
print("2 {} {}".format(i + 1, i + 2))
else:
print("1 {} {}".format(i + 1, i + 2))
# Right Side
for i in range(index, n):
if arr[i] == highest_freq_num:
continue
if arr[i] > highest_freq_num:
print("2 {} {}".format(i + 1, i))
else:
print("1 {} {}".format(i + 1, i))
solve()
``` | output | 1 | 91,768 | 12 | 183,537 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
n = int(input())
arr = list(map(int,input().split()))
counter = {}
for a in arr:
counter[a] = counter.get(a,0) + 1
need = sorted(counter.items(), key=lambda d: d[1], reverse=True)[0][0]
pre = -1
operations = []
i = 0
while i < n:
if arr[i] == need:
i += 1
continue
else:
if i > 0:
if arr[i] > need:
operations.append((2,i+1,i))
else:
operations.append((1,i+1,i))
i += 1
else:
j = i + 1
while arr[j] != need:
j += 1
i = j + 1
while j > 0:
if arr[j-1] > need:
operations.append((2,j,j+1))
else:
operations.append((1,j,j+1))
j -= 1
print(len(operations))
for i in range(len(operations)):
print('%d %d %d'%operations[i])
``` | instruction | 0 | 91,769 | 12 | 183,538 |
Yes | output | 1 | 91,769 | 12 | 183,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
from collections import deque, Counter
n = int(input())
a = [ int(x) for x in input().split()]
freq = Counter(a)
most = freq.most_common(1)[0][0]
#print(most)
ind = a.index(most)
substitutions = []
for i in range(ind-1,-1,-1):
if(a[i]>most):
substitutions.append((2,i+1,i+1+1))
elif(a[i]<most):
substitutions.append((1,i+1,i+1+1))
for i in range(ind+1,n):
if(a[i]>most):
substitutions.append((2,i+1,i+1-1))
elif(a[i]<most):
substitutions.append((1,i+1,i+1-1))
lsubst = len(substitutions)
print(lsubst)
for subst in substitutions:
for elem in subst:
print(elem,end=' ')
print()
``` | instruction | 0 | 91,770 | 12 | 183,540 |
Yes | output | 1 | 91,770 | 12 | 183,541 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
n = int(input())
num = [int(x) for x in input().split(' ')]
help = dict()
used = set()
for i in range(n):
if num[i] not in used:
help[num[i]] = 1
used.add(num[i])
else:
help[num[i]] += 1
w = 0
for i in range(n):
c = help[num[i]]
if c > w:
w = c
ans = num[i]
h = i
ans = num[h]
print(n - w)
for i in range(h, -1, -1):
if num[i] > ans:
print(2, i + 1, i + 2)
elif num[i] < ans:
print(1, i + 1, i + 2)
for i in range(h, n, 1):
if num[i] > ans:
print(2, i + 1, i)
elif num[i] < ans:
print(1, i + 1, i)
``` | instruction | 0 | 91,771 | 12 | 183,542 |
Yes | output | 1 | 91,771 | 12 | 183,543 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
n = int(input())
arr = [int(x) for x in input().split()]
# mx_range = (0, 0)
# cnt = 0
# mx_cnt = 0
# value = arr[0]
# last = 0
#
# for i, x in enumerate(arr):
# if last == x:
# cnt += 1
# if mx_cnt < cnt:
# mx_cnt = cnt
# mx_range = (i - cnt, i)
# value = x
# else:
# cnt = 0
#
# last = x
#
# print(mx_range)
#
# cnt = 0
# ops = []
#
# if mx_range[0] > 0:
# for i in range(mx_range[0] - 1, -1, -1):
# j = i + 1
#
# t = if arr[i] >
# ops.append(f'{}')
# cnt += 1
# if mx_range[1] < n - 1:
# for i in range(mx_range[1] + 1, n):
# j = i - 1
# print(i, j)
# cnt += 1
#
# print(cnt)
cnt = {}
for x in arr:
if x in cnt:
cnt[x] += 1
else:
cnt[x] = 1
mx_num, mx_cnt = max(cnt.items(), key=lambda x: x[1])
first_index = 0
for i, x in enumerate(arr):
if mx_num == x:
first_index = i
break
print(n - mx_cnt)
for i in range(first_index - 1, -1, -1):
j = i + 1
if mx_num == arr[i]:
continue
if mx_num > arr[i]:
t = 1
else:
t = 2
print(t, i+1, j+1)
for i in range(first_index + 1, n):
j = i - 1
if mx_num == arr[i]:
continue
if mx_num > arr[i]:
t = 1
else:
t = 2
print(t, i+1, j+1)
``` | instruction | 0 | 91,772 | 12 | 183,544 |
Yes | output | 1 | 91,772 | 12 | 183,545 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
input()
n = [int(x) for x in input().split()]
leader = n[0]
li = 0
c = 1
mc = 0
for i in range(1, len(n)):
if n[i] == n[i - 1]:
c += 1
else:
if mc < c:
leader = n[i - 1]
li = i - 1
mc = c
c = 1
if c > mc:
leader = n[-1]
li = len(n) - 1
i = li
k = 0
res = []
for j in range(li, len(n)):
if n[j] == leader:
continue
if n[i] < leader:
res += [[1, j + 1, j]]
n[j] = leader
k += 1
else:
k += 1
res += [[2, j + 1, j]]
n[j] = leader
for j in range(li,-1,-1):
if n[j] == leader:
continue
if n[j] < leader:
res += [[1, j + 1, j + 2]]
n[j] = leader
k += 1
else:
k += 1
res += [[2, j + 1, j + 2]]
n[j] = leader
print(k)
for e in res:
for a in e:
print(a, end=" ")
print()
``` | instruction | 0 | 91,773 | 12 | 183,546 |
No | output | 1 | 91,773 | 12 | 183,547 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
import math
import bisect
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, x, y = egcd(b % a, a)
return (g, y - (b // a) * x, x)
def mulinv(b, n):
g, x, _ = egcd(b, n)
if g == 1:
return x % n
primes = []
def isprime(n):
for d in range(2, int(math.sqrt(n))+1):
if n%d==0:
return False
return True
def argsort(ls):
return sorted(range(len(ls)), key=ls.__getitem__)
def f(p=0):
if p==1:
return map(int, input().split())
elif p==2:
return list(map(int, input().split()))
else:
return int(input())
n = f()
dl = [0]*(2*10**5+1)
pl = [list() for _ in range(2*10**5+1)]
cl = f(2)
for i in range(n):
dl[cl[i]]+=1
if i==n-1 or cl[i] != cl[i + 1]:
pl[cl[i]].append(i)
if i ==0 or cl[i] != cl[i - 1]:
pl[cl[i]].append(i)
mx = max(dl)
pos = dl.index(mx)
start = pl[pos][1]
print(n-mx)
ln = len(pl[pos])
for j in range(2, ln):
x = pl[pos][j]
for i in range(start+1, x):
if cl[i]>pos:
print(2, i+1, i)
else:
print(1, i+1, i)
if j+1<ln:
start = pl[pos][j+1]
if cl[-1]!=pos:
for i in range(pl[pos][-1]+1, n):
if cl[i] > pos:
print(2, i+1, i)
else:
print(1, i+1, i)
if cl[0]!=pos:
for i in range(pl[pos][0], 0, -1):
if cl[i-1] > pos:
print(2, i, i+1)
else:
print(1, i, i+1)
``` | instruction | 0 | 91,774 | 12 | 183,548 |
No | output | 1 | 91,774 | 12 | 183,549 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
import collections
n = int(input())
s = ['$']
s.extend(input().strip().split())
cnt = collections.Counter(s)
M = cnt.most_common(1)
most = M[0][0]
most_cnt = M[0][1]
ans = n - int(most_cnt)
print(ans)
if ans != 0:
x = s.index(M[0][0])
for i in range(2, n + 1):
if s[i - 1] == most and s[i] != most:
print("%d %d %d" % (2 if s[i] > most else 1, i, i - 1))
s[i] = most
for i in range(n - 1, 0, -1):
if s[i + 1] == most and s[i] != most:
print("%d %d %d" % (2 if s[i] > most else 1, i, i + 1))
s[i] = most
# print(s)
``` | instruction | 0 | 91,775 | 12 | 183,550 |
No | output | 1 | 91,775 | 12 | 183,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):
1. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i + |a_i - a_j|;
2. Choose a pair of indices (i, j) such that |i-j|=1 (indices i and j are adjacent) and set a_i := a_i - |a_i - a_j|.
The value |x| means the absolute value of x. For example, |4| = 4, |-3| = 3.
Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.
It is guaranteed that you always can obtain the array of equal elements using such operations.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
In the first line print one integer k — the minimum number of operations required to obtain the array of equal elements.
In the next k lines print operations itself. The p-th operation should be printed as a triple of integers (t_p, i_p, j_p), where t_p is either 1 or 2 (1 means that you perform the operation of the first type, and 2 means that you perform the operation of the second type), and i_p and j_p are indices of adjacent elements of the array such that 1 ≤ i_p, j_p ≤ n, |i_p - j_p| = 1. See the examples for better understanding.
Note that after each operation each element of the current array should not exceed 10^{18} by absolute value.
If there are many possible answers, you can print any.
Examples
Input
5
2 4 6 6 6
Output
2
1 2 3
1 1 2
Input
3
2 8 10
Output
2
2 2 1
2 3 2
Input
4
1 1 1 1
Output
0
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
dict={}
for i in l:
try:
dict[i]+=1
except:
dict[i]=1
maks=0
eleman=0
for i in dict:
if dict[i]>maks:
maks=dict[i]
eleman=i
indeks=[-1]
for i in range(n):
if l[i] == eleman:
indeks.append(i)
indeks.append(n)
print(n-maks)
for i in range(maks+2):
t=indeks[i]
while t-1 > indeks[i-1]:
if l[t-1] < eleman:
print(1,t,t+1)
elif l[t-1]>eleman:
print(2,t,t+1)
t-=1
``` | instruction | 0 | 91,776 | 12 | 183,552 |
No | output | 1 | 91,776 | 12 | 183,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,812 | 12 | 183,624 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
n,k=map(int,input().split())
b=list(map(int,input().split()))
a=[];dic={}
for i in b:
try:
if dic[i]:
dic[i]+=1
except KeyError:
dic[i]=1
a.append(i)
a.sort()
i=0;j=len(a)-1
while(1):
frst=a[i];last=a[j]
if frst==last:
break
fcost=dic[frst];lcost=dic[last]
fnxt=a[i+1];lnxt=a[j-1]
fnxtcost=fnxt-frst;lnxtcost=last-lnxt
ftotal=fcost*fnxtcost;ltotal=lcost*lnxtcost
if fcost<=lcost:
if ftotal<=k:
i+=1;k-=ftotal;dic[fnxt]+=fcost;dic[frst]=0
else:
temp=k//fcost
frst+=temp;
k-=(k*temp)
break
else:
if ltotal<=k:
j-=1;k-=ltotal;dic[lnxt]+=lcost;dic[last]=0
else:
temp=k//lcost
k-=(k*temp)
last-=temp
break
print(last-frst)
``` | output | 1 | 91,812 | 12 | 183,625 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,813 | 12 | 183,626 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
# ------------------- fast io --------------------
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")
# ------------------- fast io --------------------
import bisect
n,k=map(int,input().split())
vals=list(map(int,input().split()))
vals.sort()
a1=[]
a2=[]
if n%2==1:
moves=0
for s in range(n//2):
moves+=abs(vals[s+1]-vals[s])*(s+1)
moves+=abs(vals[-1-(s+1)]-vals[-1-s])*(s+1)
a1.append(moves)
a2.append(abs(vals[s+1]-vals[-1-(s+1)]))
else:
moves=0
for s in range(n//2 -1):
moves+=abs(vals[s+1]-vals[s])*(s+1)
moves+=abs(vals[-1-(s+1)]-vals[-1-s])*(s+1)
a1.append(moves)
a2.append(abs(vals[s+1]-vals[-1-(s+1)]))
moves+=abs(vals[n//2 -1]-vals[n//2])*(n//2)
a1.append(moves)
a2.append(0)
ind=bisect.bisect_left(a1,k)
if k<a1[0]:
print(abs(vals[0]-vals[-1])-k)
elif k>=a1[-1]:
print(0)
else:
if k==a1[ind]:
print(a2[ind])
elif k<a1[ind]:
print(a2[ind-1]-(k-a1[ind-1])//(ind+1))
``` | output | 1 | 91,813 | 12 | 183,627 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,814 | 12 | 183,628 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
import sys
input=sys.stdin.readline
import collections
from collections import defaultdict
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
l.sort()
freq=defaultdict(list)
for i in l:
if i not in freq:
freq[i].append(1)
else:
freq[i][0]+=1
l=list(set(l))
l.sort()
#print(freq,l)
count=0
beg=0
last=(len(l)-1)
diff =l[last]-l[beg]
while count<k and diff>0:
if freq[l[beg]]<freq[l[last]]:
if (count+freq[l[beg]][0])<=k and diff>=0:
diff-=min((k-count)//freq[l[beg]][0],(l[beg+1]-l[beg]))
count+=min(k-count,freq[l[beg]][0]*(l[beg+1]-l[beg]))
freq[l[beg+1]][0]+=freq[l[beg]][0]
beg+=1
#print(freq,diff,count,beg)
else:
count=k
else:
if (count+freq[l[last]][0])<=k and diff>=0:
diff-=min((k-count)//freq[l[last]][0],(l[last]-l[last-1]))
count+=min(k-count,freq[l[last]][0]*(l[last]-l[last-1]))
#print(freq[l[last-1]])
freq[l[last-1]][0]+=freq[l[last]][0]
#print(freq[l[last-1]])
last-=1
#print(freq,diff,count,last)
else:
count=k
print(diff)
``` | output | 1 | 91,814 | 12 | 183,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,815 | 12 | 183,630 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
from sys import stdin
input = iter(stdin.buffer.read().decode().splitlines()).__next__
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
left = 1
right = 1
while 1:
if left*(a[left]-a[left-1]) <= k:
k -= left*(a[left]-a[left-1])
left += 1
else:
print(a[n-right]-a[left-1]-int(k/left))
exit()
if left+right > n:
print(0)
exit()
if right*(a[n-right]-a[n-1-right]) <= k:
k -= right*(a[n-right]-a[n-1-right])
right += 1
else:
print(a[n-right]-a[left-1]-int(k/right))
exit()
if left+right > n:
print(0)
exit()
``` | output | 1 | 91,815 | 12 | 183,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,816 | 12 | 183,632 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
n,k=map(int,input().strip().split())
a =list(map(int,input().strip().split()))
from collections import deque, Counter
temp = Counter(a)
a = deque(sorted(map(lambda x: [x,temp[x]], temp)))
# print(a)
while( (k>=a[0][1] or k>=a[-1][1] ) and len(a)>1):
# forward
cf,f,smf = a[0][1], a[0][0], a[1][0]
cr,r,smr = a[-1][1], a[-1][0], a[-2][0]
if(cf < cr):
# front should be removed
temp = cf*(smf-f)
if(temp<=k):
k-=temp
a.popleft()
a[0][1]+=cf
else:
factor=k//cf
temp1 = factor*cf
k-=temp1
a[0][0]=f+factor
else:
# back should be removed
temp = cr*(r-smr)
if(temp<=k):
k-=temp
a.pop()
a[-1][1]+=cr
else:
factor=k//cr
temp1 = factor*cr
k-=temp1
a[-1][0]=r-factor
print(max(a)[0]-min(a)[0])
``` | output | 1 | 91,816 | 12 | 183,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,817 | 12 | 183,634 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
# encoding: utf-8
from sys import stdin
n, k = [int(i) for i in stdin.readline().strip().split()]
a = [int(i) for i in stdin.readline().strip().split()]
a.sort()
i = 0
j = n - 1
a_min = a[i]
a_max = a[j]
while i < j:
if i + 1 <= n - j:
required_steps = (a[i + 1] - a[i]) * (i + 1)
if required_steps > k:
a_min += k // (i + 1)
break
else:
i += 1
k -= required_steps
a_min = a[i]
else:
required_steps = (a[j] - a[j - 1]) * (n - j)
if required_steps > k:
a_max -= k // (n - j)
break
else:
j -= 1
k -= required_steps
a_max = a[j]
print(a_max - a_min)
``` | output | 1 | 91,817 | 12 | 183,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,818 | 12 | 183,636 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
import sys
import math
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
A.sort()
l=0
r=n-1
while(l<r and k):
temp=((A[l+1]-A[l])*(l+1)+(A[r]-A[r-1])*(n-r))
if(k>=temp):
k-=temp
l+=1
r-=1
else:
ans=A[r]-A[l]-(k//(l+1))
print(ans)
sys.exit(0)
print(0)
``` | output | 1 | 91,818 | 12 | 183,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations. | instruction | 0 | 91,819 | 12 | 183,638 |
Tags: binary search, constructive algorithms, greedy, sortings, ternary search, two pointers
Correct Solution:
```
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int,minp().split())
def solve():
n, k = mints()
a = list(mints())
a.sort()
b = []
c = []
i = 0
while i < n:
j = i + 1
while j < n and a[j] == a[i]:
j += 1
b.append(a[i])
c.append(j-i)
i = j
l = 0
r = len(b) - 1
while l < r and k > 0:
if c[l] < c[r]:
x = min(b[l] + k // c[l], b[l+1])
k -= (x - b[l]) * c[l]
if x == b[l+1]:
c[l+1] += c[l]
l += 1
else:
b[l] = x
break
else:
x = max(b[r] - k // c[r], b[r-1])
k -= (b[r] - x) * c[r]
if x == b[r-1]:
c[r-1] += c[r]
r -= 1
else:
b[r] = x
break
print(b[r]-b[l])
solve()
``` | output | 1 | 91,819 | 12 | 183,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations.
Submitted Solution:
```
import sys
from bisect import bisect_left,bisect_right
input=sys.stdin.readline
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
i=0;j=n-1
res=a[j]-a[i]
while res>0:
if i==j-1:
used=(a[i+1]-a[i])*(i+1)
else:
used=(a[i+1]-a[i]+a[j]-a[j-1])*(i+1)
if used>=k:
res-=k//(i+1)
break
k-=used
if i==j-1:
res=0
break
res=a[j-1]-a[i+1]
j-=1;i+=1
print(res)
``` | instruction | 0 | 91,822 | 12 | 183,644 |
Yes | output | 1 | 91,822 | 12 | 183,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations.
Submitted Solution:
```
n,k=map(int, input().split(' '))
a=[]
for i in input().split(): a.append(int(i))
a=sorted(a)
rez=0
for i in range(k):
if a[-1]-a[0]>1:
a[0]+=1
a=sorted(a)
rez=a[-1]-a[0]-1
else:
rez=0
break
print(rez)
``` | instruction | 0 | 91,825 | 12 | 183,650 |
No | output | 1 | 91,825 | 12 | 183,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.
Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{14}) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.
The second line contains a sequence of integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^{9}).
Output
Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.
Examples
Input
4 5
3 1 7 5
Output
2
Input
3 10
100 100 100
Output
0
Input
10 9
4 5 5 7 5 4 5 2 4 3
Output
1
Note
In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3, 3, 5, 5], and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.
In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations.
Submitted Solution:
```
n,m=map(int,input().split())
l=list(map(int,input().split()))
i=1
s=sum(l)
while (i<=m)and(l[0]!=l[n-1]):
l.sort()
a=l[n-1]
b=l[0]
v=s/n
h1=a-v
h2=v-b
if h1>=h2:
l[n-1]-=1
else :
l[0]+=1
i=i+1
l.sort()
print(l[n-1]-l[0])
``` | instruction | 0 | 91,827 | 12 | 183,654 |
No | output | 1 | 91,827 | 12 | 183,655 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,860 | 12 | 183,720 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
for t in range(int(input())):
d,m = [int(i) for i in input().split()]
tot = 0
p = 1
while p<=d:
p *= 2
p //= 2
while d>0:
tot += (d-p+1)*(tot+1)
tot %= m
d = p-1
p //= 2
print(tot)
``` | output | 1 | 91,860 | 12 | 183,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,861 | 12 | 183,722 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
a=[1]
for i in range(30):
a.append(a[-1]*2)
for t in range(int(input())):
d,m=map(int,input().split())
ans=1
z=d.bit_length()
for i in range(z):
if i!=z-1:
ans=(ans*((a[i+1]-1)-a[i]+2))%m
else:
ans=(ans*(d-a[i]+2))%m
print((ans-1)%m)
``` | output | 1 | 91,861 | 12 | 183,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,862 | 12 | 183,724 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
import sys
input = sys.stdin.readline
t=int(input())
D=[(1<<i)-1 for i in range(1,32)]
for tests in range(t):
d,m=map(int,input().split())
ANS=[1] # answer for 1, 3, 7, 15,
B=[0]
for i in range(1,32):
B.append(ANS[-1]+1)
ANS.append((ANS[-1]+B[-1]*(1<<(i)))%m)
for i in range(32):
if d<D[i]:
break
#print(ANS,B)
#print(i)
A=ANS[i-1]
#print(i,A)
A=(A+(d-D[i-1])*B[i])%m
print(A)
``` | output | 1 | 91,862 | 12 | 183,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,863 | 12 | 183,726 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
for i in range(int(input())):
d, m = map(int, input().split())
a, k, s = [], 1, 1
while s < d:
a.append(k)
k <<= 1
s += k
a.append(d - s + k)
dp = [1]
for j in range(len(a)):
dp.append((a[j] + 1) * dp[-1])
print((dp[-1] - 1) % m)
``` | output | 1 | 91,863 | 12 | 183,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,864 | 12 | 183,728 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
t = int(input())
for _ in range(t):
d, m = map(int, input().split())
a = []
i = 0
while d > (1<<(i+1))-1:
a.append(1<<i)
i += 1
a.append((1<<i) - (1<<(i+1)) + d + 1)
#print(a)
ans = 1
for x in a:
ans *= x+1
ans %= m
print((ans-1)%m)
``` | output | 1 | 91,864 | 12 | 183,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,865 | 12 | 183,730 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
t = int(input())
for _ in range(t):
d, m = map(int, input().split())
a = []
i = 0
while d > (1<<(i+1))-1:
a.append(1<<i)
i += 1
a.append((1<<i) - (1<<(i+1)) + d + 1)
#print(a,(1<<i) - (1<<(i+1)),d+1)
ans = 1
for x in a:
ans *= x+1
ans %= m
#print(ans)
print((ans-1)%m)
``` | output | 1 | 91,865 | 12 | 183,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,866 | 12 | 183,732 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
q = int(input())
for i in range(q):
inp = input().split()
p = int(inp[0])
n = int(inp[1])
e = 1
a = 1
t = 0
while e*2 <= p:
t += e*a
e *= 2
a = t+1
t %= n
t += (p-e+1)*a
t %= n
print(t)
``` | output | 1 | 91,866 | 12 | 183,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 91,867 | 12 | 183,734 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
arr=[[1,1,1],[2,3,2],[4,11,6],[8,59,30],[16,539,270],[32,9179,4590],[64,302939,151470],[128,19691099,9845550],[256,2540151899,1270075950],[512,652819038299,326409519150],[1024,334896166647899,167448083323950],[2048,343268570814097499,171634285407048750],[4096,703357301598085777499,351678650799042888750],[8192,2881654864647357430417499,1440827432323678715208750],[16384,23609398306055799427410577499,11804699153027899713705288750],[32768,386839991244724273618122312337499,193419995622362136809061156168750],[65536,12676359673098369722192250052987537499,6338179836549184861096125026493768750],[131072,830770583895847856483313491722644245137499,415385291947923928241656745861322122568750],[262144,108891592742980466092837349300562149142907537499,54445796371490233046418674650281074571453768750],[524288,28545386579608614283906846932395864587067496417937499,14272693289804307141953423466197932293533748208968750],[1048576,14966032184436420774295236871338895448489030629464033937499,7483016092218210387147618435669447724244515314732016968750],[2097152,15693037129859788786248176592837924972690282270351508314081937499,7846518564929894393124088296418962486345141135175754157040968750],[4194304,32910699895996845632446722286199832870252343534114476715401877473937499,16455349947998422816223361143099916435126171767057238357700938736968750],[8388608,138037513127279049620399449518619390006863755746854020259793671698323425937499,69018756563639524810199724759309695003431877873427010129896835849161712968750],[16777216,1157942724957111181157129405826936282586087363231861356037487532551601175730145937499,578971362478555590578564702913468141293043681615930678018743766275800587865072968750],[33554432,19427056370176769979399471138639574458120108873898759284155188468412776622679791835105937499,9713528185088384989699735569319787229060054436949379642077594234206388311339895917552968750],[67108864,651863861360319626430170934556915312303502499161941367183313233065729129529675355585009227745937499,325931930680159813215085467278457656151751249580970683591656616532864564764837677792504613872968750],[134217728,43745843870408406166952773174103864509808588243421336348220398010521552280174496931781380288556373985937499,21872921935204203083476386587051932254904294121710668174110199005260776140087248465890690144278186992968750],[268435456,5871467817474786578237996065680342304630206938728110954803295012448320852599792701901164786815401204867209185937499,2935733908737393289118998032840171152315103469364055477401647506224160426299896350950582393407700602433604592968750],[536870912,1576110146844636921106782728655104702459842815602051462899328841772385696950254992040103938381099797066879922141730785937499,788055073422318460553391364327552351229921407801025731449664420886192848475127496020051969190549898533439961070865392968750]]
t=int(input())
for _ in range(t):
[d,m]=[int(x) for x in input().split()]
for p in arr:
if d>=p[0] and d<p[0]*2:
print((p[1]+p[2]*(d-p[0]))%m)
``` | output | 1 | 91,867 | 12 | 183,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,876 | 12 | 183,752 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from bisect import *
from collections import *
from math import gcd,ceil,sqrt,floor,inf
from heapq import *
from itertools import *
#from operator import add,mul,sub,xor,truediv,floordiv
from functools import *
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
#------------------------------------------------------------------------
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
farr=[1]
ifa=[]
def fact(x,mod=0):
if mod:
while x>=len(farr):
farr.append(farr[-1]*len(farr)%mod)
else:
while x>=len(farr):
farr.append(farr[-1]*len(farr))
return farr[x]
def ifact(x,mod):
global ifa
fact(x,mod)
ifa.append(pow(farr[-1],mod-2,mod))
for i in range(x,0,-1):
ifa.append(ifa[-1]*i%mod)
ifa.reverse()
def per(i,j,mod=0):
if i<j: return 0
if not mod:
return fact(i)//fact(i-j)
return farr[i]*ifa[i-j]%mod
def com(i,j,mod=0):
if i<j: return 0
if not mod:
return per(i,j)//fact(j)
return per(i,j,mod)*ifa[j]%mod
def catalan(n):
return com(2*n,n)//(n+1)
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def lowbit(n):
return n&-n
def inverse(a,m):
a%=m
if a<=1: return a
return ((1-inverse(m,a)*m)//a)%m
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
'''
class SMT:
def __init__(self,arr):
self.n=len(arr)-1
self.arr=[0]*(self.n<<2)
self.lazy=[0]*(self.n<<2)
def Build(l,r,rt):
if l==r:
self.arr[rt]=arr[l]
return
m=(l+r)>>1
Build(l,m,rt<<1)
Build(m+1,r,rt<<1|1)
self.pushup(rt)
Build(1,self.n,1)
def pushup(self,rt):
self.arr[rt]=self.arr[rt<<1]+self.arr[rt<<1|1]
def pushdown(self,rt,ln,rn):#lr,rn表区间数字数
if self.lazy[rt]:
self.lazy[rt<<1]+=self.lazy[rt]
self.lazy[rt<<1|1]+=self.lazy[rt]
self.arr[rt<<1]+=self.lazy[rt]*ln
self.arr[rt<<1|1]+=self.lazy[rt]*rn
self.lazy[rt]=0
def update(self,L,R,c,l=1,r=None,rt=1):#L,R表示操作区间
if r==None: r=self.n
if L<=l and r<=R:
self.arr[rt]+=c*(r-l+1)
self.lazy[rt]+=c
return
m=(l+r)>>1
self.pushdown(rt,m-l+1,r-m)
if L<=m: self.update(L,R,c,l,m,rt<<1)
if R>m: self.update(L,R,c,m+1,r,rt<<1|1)
self.pushup(rt)
def query(self,L,R,l=1,r=None,rt=1):
if r==None: r=self.n
#print(L,R,l,r,rt)
if L<=l and R>=r:
return self.arr[rt]
m=(l+r)>>1
self.pushdown(rt,m-l+1,r-m)
ans=0
if L<=m: ans+=self.query(L,R,l,m,rt<<1)
if R>m: ans+=self.query(L,R,m+1,r,rt<<1|1)
return ans
'''
class DSU:#容量+路径压缩
def __init__(self,n):
self.c=[-1]*n
def same(self,x,y):
return self.find(x)==self.find(y)
def find(self,x):
if self.c[x]<0:
return x
self.c[x]=self.find(self.c[x])
return self.c[x]
def union(self,u,v):
u,v=self.find(u),self.find(v)
if u==v:
return False
if self.c[u]>self.c[v]:
u,v=v,u
self.c[u]+=self.c[v]
self.c[v]=u
return True
def size(self,x): return -self.c[self.find(x)]
class UFS:#秩+路径
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
seen.add(u)
for v,w in graph[u]:
if v not in d or d[v]>d[u]+w:
d[v]=d[u]+w
heappush(heap,(d[v],v))
return d
def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)]
class DLN:
def __init__(self,val):
self.val=val
self.pre=None
self.next=None
def nb(i,j):
for ni,nj in [[i+1,j],[i-1,j],[i,j-1],[i,j+1]]:
if 0<=ni<n and 0<=nj<m:
yield ni,nj
@bootstrap
def gdfs(r,p):
if len(g[r])==1 and p!=-1:
yield None
for ch in g[r]:
if ch!=p:
yield gdfs(ch,r)
yield None
def lcm(a,b):
return a*b//gcd(a,b)
t=N()
for i in range(t):
n,k=RL()
f=False
a=RLL()
ans='no'
for i in range(n):
if a[i]<k:
a[i]=0
elif a[i]==k:
f=True
if f:
if n<2:
ans='yes'
else:
pre=-inf
f2=False
for i in range(n):
if a[i]:
if i-pre<3:
f2=True
break
pre=i
if f2:
ans='yes'
print(ans)
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thread(target=main)
t.start()
t.join()
'''
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thread(target=main)
t.start()
t.join()
'''
``` | output | 1 | 91,876 | 12 | 183,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,877 | 12 | 183,754 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key,lru_cache
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# import sys
# input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip().split()]
def st():return str(input().rstrip())[2:-1]
def val():return int(input().rstrip())
def li2():return [str(i)[2:-1] for i in input().rstrip().split()]
def li3():return [int(str(i)[2:-1]) for i in input().rstrip()]
def do2(l):
for i in range(2,len(l) + 1):
if sum(l[i-2:i + 1]) > 0 or sum(l[i-2:i]) > 0:return 1
return 0
for _ in range(int(input())):
n,k = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
if k not in l:
print('NO')
continue
if n == 1:
print('YES')
continue
l = [(-1 if i < k else 1) for i in l]
print('YES' if do2(l) else 'NO')
``` | output | 1 | 91,877 | 12 | 183,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,878 | 12 | 183,756 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
input=sys.stdin.buffer.readline
t=int(input())
for _ in range(t):
n,k=[int(x) for x in input().split()]
arr=[int(x) for x in input().split()]
arr.append(-1)
arr.append(-1)
if k not in arr:
print("no")
continue
elif n == 1:
if arr[0] == k:
print("yes")
continue
flag=0
for i in range(n):
if arr[i] >= k:
if arr[i+1] >= k or arr[i+2] >= k:
flag=1
if(flag):
print("yes")
else:
print("no")
``` | output | 1 | 91,878 | 12 | 183,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,879 | 12 | 183,758 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
ans = []
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k not in a:
ans.append('no')
continue
if n == 1:
ans.append('yes' if a[0] == k else 'no')
continue
good = False
for i in range(n - 1):
if a[i] >= k and a[i + 1] >= k:
good = True
for i in range(1, n):
if a[i] >= k and a[i - 1] >= k:
good = True
for i in range(1, n - 1):
if a[i - 1] >= k and a[i + 1] >= k:
good = True
ans.append('yes' if good else 'no')
print('\n'.join(ans))
``` | output | 1 | 91,879 | 12 | 183,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,880 | 12 | 183,760 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
t = int(input())
for u in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k not in a:
print("no")
continue
if n == 1:
print("yes")
continue
if n == 2:
if (a[0] + a[1]) / 2 >= k:
print("yes")
continue
x = y = z = 0
ok = 0
for i in range(n):
if a[i] > k:
x += 1
elif a[i] < k:
y += 1
else:
z += 1
if i >= 3:
if a[i - 3] > k:
x -= 1
elif a[i - 3] < k:
y -= 1
else:
z -= 1
if i >= 2:
if y >= 2:
continue
ok = 1
print("yes")
break
if not ok:
print("no")
``` | output | 1 | 91,880 | 12 | 183,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,881 | 12 | 183,762 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
input = sys.stdin.readline
def do2(l):
for i in range(2,len(l) + 1):
if sum(l[i-2:i + 1]) > 0 or sum(l[i-2:i]) > 0:return 1
return 0
for _ in range(int(input())):
n,k = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
if k not in l:
print('NO')
continue
if n == 1:
print('YES')
continue
l = [(-1 if i < k else 1) for i in l]
print('YES' if do2(l) else 'NO')
``` | output | 1 | 91,881 | 12 | 183,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,882 | 12 | 183,764 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
if k not in a:
print("no")
continue
res = False
for i, x in enumerate(a):
if x >= k:
if i - 1 >= 0 and a[i - 1] >= k:
res = True
break
if i + 1 < n and a[i + 1] >= k:
res = True
break
if res:
print("yes")
continue
if a == [k]:
print("yes")
continue
idx = [i for i in range(n) if a[i] >= k]
for j in range(len(idx) - 1):
if idx[j] + 2 == idx[j + 1]:
res = True
break
if res:
print("yes")
continue
print("no")
``` | output | 1 | 91,882 | 12 | 183,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 91,883 | 12 | 183,766 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
def ri(): return int(input())
def ria(): return list(map(int, input().split()))
def solve(n, k, a):
flag = False
for i in range(n):
if a[i] < k:
a[i] = 0
elif a[i] > k:
a[i] = 2
else:
a[i] = 1
if a[i] == 1:
flag = True
if not flag:
return False
if n == 1:
return True
for i in range(n):
for j in range(i+1, i+3):
if j < n and a[i] and a[j]:
return True
return False
def main():
for _ in range(ri()):
n, k = ria()
a = ria()
print('yes' if solve(n, k, a) else 'no')
if __name__ == '__main__':
main()
``` | output | 1 | 91,883 | 12 | 183,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,907 | 12 | 183,814 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
ans=[]
if sum(a)%n!=0:
print(-1)
continue
b=sum(a)//n
for i in range(1,n):
if a[i]%(i+1)==0:
ans.append([i+1,1,a[i]//(i+1)])
a[0]+=a[i]
a[i]=0
else:
x=i+1-a[i]%(i+1)
ans.append([1,i+1,x])
a[0]-=x
a[i]+=x
ans.append([i+1,1,a[i]//(i+1)])
a[0]+=a[i]
a[i]=0
for i in range(1,n):
ans.append([1,i+1,b])
l=len(ans)
print(l)
for i in range(l):
print(" ".join(map(str,ans[i])))
``` | output | 1 | 91,907 | 12 | 183,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,908 | 12 | 183,816 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
x = sum(arr)
if x%n:
print(-1)
else:
print(3*n-3)
val = x//n
arr1 = arr[:]
for i in range(1,n):
z = arr[i]%(i+1)
if z != 0:
print(1,i+1,i+1-z)
arr1[i] += i+1-z
else:
print(1,i+1,0)
print(i+1,1,arr1[i]//(i+1))
arr1[i] = 0
arr1[0] += arr[i]
#print(arr1)
for i in range(1,n):
print(1,i+1,val)
#arr1[0] -= val
#arr1[i] += val
#print(arr1)
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
``` | output | 1 | 91,908 | 12 | 183,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,909 | 12 | 183,818 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil,pi,sin,cos,acos,atan
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush,heappop,heapify
from itertools import permutations,combinations
mod = int(1e9)+7
ip = lambda : int(stdin.readline())
inp = lambda: map(int,stdin.readline().split())
ips = lambda: stdin. readline().rstrip()
out = lambda x : stdout.write(str(x)+"\n")
t = ip()
for _ in range(t):
n = ip()
arr = list(inp())
if sum(arr)%n == 0:
mean = sum(arr)//n
ans = []
for i in range(1,n):
idx = i+1
if arr[i]%idx == 0:
quot = arr[i]//idx
arr[0] += arr[i]
arr[i] = 0
query = "{} {} {}".format(idx,1,quot)
ans.append(query)
for i in range(1,n):
idx = i+1
if arr[i] == 0:
pass
else:
if arr[i]%idx != 0:
val = idx - arr[i]%idx
arr[0] -= val
arr[i] += val
query = "{} {} {}".format(1,idx,val)
ans.append(query)
quot = arr[i]//idx
arr[0] += arr[i]
arr[i] = 0
query = "{} {} {}".format(idx,1,quot)
ans.append(query)
for i in range(1,n):
idx = i+1
arr[i] += mean
arr[0] -= mean
query = "{} {} {}".format(1,idx,mean)
ans.append(query)
print(len(ans))
for i in ans:
print(i)
else:
out(-1)
``` | output | 1 | 91,909 | 12 | 183,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,910 | 12 | 183,820 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import math
t=int(input())
for _ in range(t):
n=int(input())
arr=list(map(int,input().split()))
a=sum(arr)
if a%n!=0:
print(-1)
else:
b=a//n
ans=[]
for i in range(1,n):
x=arr[i]%(i+1)
if x!=0:
ans.append([1,i+1,i+1-x])
arr[i]+=i+1-x
y=arr[i]//(i+1)
ans.append([i+1,1,y])
arr[0]+=arr[i]
arr[i]=0
for i in range(1,n):
ans.append([1,i+1,b])
l=len(ans)
print(l)
for i in range(l):
print(" ".join(str(x) for x in ans[i]))
``` | output | 1 | 91,910 | 12 | 183,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,911 | 12 | 183,822 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import math
import sys
input=sys.stdin.readline
from collections import Counter, defaultdict, deque
def f(n, a):
sa = sum(a)
if sa%n != 0:
return [-1]
stps = 0
#make each a[i] from i = 2 to n equal to multiple of i
#max value to add will be i - 1 and subtract that from a[1]
#now make a[i] to zero and add that to a[1]
#now all elements are 0 except a[1] == sum(a) in max 2*(n-1) steps till now
#make all elements from 2 to n-1 equal to sum(a)/n in (n-1) steps
#so total max steps is 3(n-1) < 3n
a.insert(0, 0)
ans = []
for i in range(2, n+1):
if a[i] % i == 0:
delta = 0
else:
delta = i - a[i]%i
pi = 1
pj = i
x = delta
stps += 1
ans.append([pi, pj, x])
a[1] -= delta
a[i] += delta
pi = i
pj = 1
x = (a[i]) // i
ans.append([pi, pj, x])
stps += 1
a[i] = 0
a[1] += a[i]
#print(a[1], a[i])
for i in range(1, n+1):
ans.append([1, i, sa // n])
stps += 1
return [stps, ans]
t = int(input())
result = []
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
result.append(f(n, a))
for i in range(t):
if result[i][0] == -1:
print(result[i][0])
else:
print(result[i][0])
for j in range(result[i][0]):
print(result[i][1][j][0], result[i][1][j][1], result[i][1][j][2], sep = " ")
``` | output | 1 | 91,911 | 12 | 183,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,912 | 12 | 183,824 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
operations = []
def operate(i, j, x, A):
global operations
A[i-1] -= i * x
A[j-1] += i * x
operations.append([i, j, x])
t = int(input())
for _ in range(t):
n = int(input())
A = [int(a) for a in input().split()]
s = sum(A)
if s % n != 0:
print(-1)
continue
target = s // n
# phase 1: make A[0] zero
for i, a in enumerate(A):
if i == 0:
continue
if a % (i + 1) != 0:
operate(1, i + 1, (i + 1) - (a % (i + 1)), A)
operate(i + 1, 1, A[i] // (i + 1), A)
# phase 2: distribute A[0] to others
for i, a in enumerate(A):
operate(1, i + 1, target, A)
print(len(operations))
for operation in operations:
print(' '.join([str(o) for o in operation]))
operations = []
``` | output | 1 | 91,912 | 12 | 183,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,913 | 12 | 183,826 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
mean=sum(a)//n
if sum(a)%n!=0:
print(-1)
else:
print(3*(n-1))
for i in range(1, n):
if a[i]%(i+1)!=0:
dif=(i+1)-a[i]%(i+1)
else:
dif=0
print(1, i+1, dif)
print(i+1, 1, (a[i]+dif)//(i+1))
for i in range(1, n):
print(1, i+1, mean)
``` | output | 1 | 91,913 | 12 | 183,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n positive integers, numbered from 1 to n. You can perform the following operation no more than 3n times:
1. choose three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9);
2. assign a_i := a_i - x ⋅ i, a_j := a_j + x ⋅ i.
After each operation, all elements of the array should be non-negative.
Can you find a sequence of no more than 3n operations after which all elements of the array are equal?
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
The first line of each test case contains one integer n (1 ≤ n ≤ 10^4) — the number of elements in the array. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 10^4.
Output
For each test case print the answer to it as follows:
* if there is no suitable sequence of operations, print -1;
* otherwise, print one integer k (0 ≤ k ≤ 3n) — the number of operations in the sequence. Then print k lines, the m-th of which should contain three integers i, j and x (1 ≤ i, j ≤ n; 0 ≤ x ≤ 10^9) for the m-th operation.
If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize k.
Example
Input
3
4
2 16 4 18
6
1 2 3 4 5 6
5
11 19 1 1 3
Output
2
4 1 2
2 3 3
-1
4
1 2 4
2 4 5
2 3 3
4 5 1 | instruction | 0 | 91,914 | 12 | 183,828 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def solve_case():
n = int(input())
a = [0] + [int(x) for x in input().split()]
s = sum(a)
if s % n != 0:
print(-1)
return
print(3 * n - 3)
for i in range(2, n+1):
if a[i] % i != 0:
print('{} {} {}'.format(1, i, i - (a[i] % i)))
else:
print('{} {} {}'.format(1, i, 0))
print('{} {} {}'.format(i, 1, (a[i] // i) + (a[i] % i != 0)))
for i in range(2, n+1):
print('{} {} {}'.format(1, i, s // n))
def main():
for _ in range(int(input())):
solve_case()
main()
``` | output | 1 | 91,914 | 12 | 183,829 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.