message stringlengths 2 433k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers.
There are k boxes numbered from 1 to k. The i-th box contains n_i integer numbers. The integers can be negative. All of the integers are distinct.
Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, k integers in total. Then he will insert the chosen numbers β one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box.
Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be?
Input
The first line contains a single integer k (1 β€ k β€ 15), the number of boxes.
The i-th of the next k lines first contains a single integer n_i (1 β€ n_i β€ 5 000), the number of integers in box i. Then the same line contains n_i integers a_{i,1}, β¦, a_{i,n_i} (|a_{i,j}| β€ 10^9), the integers in the i-th box.
It is guaranteed that all a_{i,j} are distinct.
Output
If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output k lines. The i-th of these lines should contain two integers c_i and p_i. This means that Ujan should pick the integer c_i from the i-th box and place it in the p_i-th box afterwards.
If there are multiple solutions, output any of those.
You can print each letter in any case (upper or lower).
Examples
Input
4
3 1 7 4
2 3 2
2 8 5
1 10
Output
Yes
7 2
2 3
5 1
10 4
Input
2
2 3 -2
2 -1 5
Output
No
Input
2
2 -10 10
2 0 -20
Output
Yes
-10 2
-20 1
Note
In the first sample, Ujan can put the number 7 in the 2nd box, the number 2 in the 3rd box, the number 5 in the 1st box and keep the number 10 in the same 4th box. Then the boxes will contain numbers \{1,5,4\}, \{3, 7\}, \{8,2\} and \{10\}. The sum in each box then is equal to 10.
In the second sample, it is not possible to pick and redistribute the numbers in the required way.
In the third sample, one can swap the numbers -20 and -10, making the sum in each box equal to -10.
Submitted Solution:
```
def main():
k = int(input())
n = []
a = []
for i in range(k):
line = [int(x) for x in input().split()]
ni = line[0]
ai = []
n.append(ni)
a.append(ai)
for j in range(ni):
ai.append(line[1 + j])
answer, c, p = solve(k, n, a)
if answer:
print("Yes")
for i in range(k):
print(c[i], p[i] + 1)
else:
print("No")
def solve(k, n, a):
asum, sums = calc_sums(k, n, a)
if asum % k != 0:
return False, None, None
tsum = asum / k
num_map = build_num_map(k, n, a)
masks = [None]*(1 << k)
answer = [False]*(1 << k)
left = [0]*(1 << k)
right = [0]*(1 << k)
for i in range(k):
for j in range(n[i]):
found, mask, path = find_cycle(i, j, i, j, k, n, a, sums, tsum, num_map, 0, dict())
if found:
answer[mask] = True
masks[mask] = path
for mask_right in range(1 << k):
if not masks[mask_right]:
continue
zeroes_count = 0
for u in range(k):
if (1 << u) > mask_right:
break
if (mask_right & (1 << u)) == 0:
zeroes_count += 1
for mask_mask in range(1 << zeroes_count):
mask_left = 0
c = 0
for u in range(k):
if (1 << u) > mask_right:
break
if (mask_right & (1 << u)) == 0:
if (mask_mask & (1 << c)) != 0:
mask_left = mask_left | (1 << u)
c += 1
joint_mask = mask_left | mask_right
if answer[mask_left] and not answer[joint_mask]:
answer[joint_mask] = True
left[joint_mask] = mask_left
right[joint_mask] = mask_right
if joint_mask == ((1 << k) - 1):
return build_answer(k, masks, left, right)
if answer[(1 << k) - 1]:
return build_answer(k, masks, left, right)
return False, None, None
def build_answer(k, masks, left, right):
c = [-1] * k
p = [-1] * k
pos = (1 << k) - 1
while not masks[pos]:
for key, val in masks[right[pos]].items():
c[key] = val[0]
p[key] = val[1]
pos = left[pos]
for key, val in masks[pos].items():
c[key] = val[0]
p[key] = val[1]
return True, c, p
def build_num_map(k, n, a):
result = dict()
for i in range(k):
for j in range(n[i]):
result[a[i][j]] = (i, j)
return result
def find_cycle(i_origin, j_origin, i, j, k, n, a, sums, tsum, num_map, mask, path):
if (mask & (1 << i)) != 0:
if i == i_origin and j == j_origin:
return True, mask, path
else:
return False, None, None
mask = mask | (1 << i)
a_needed = tsum - (sums[i] - a[i][j])
if a_needed not in num_map:
return False, None, None
i_next, j_next = num_map[a_needed]
path[i_next] = (a[i_next][j_next], i)
return find_cycle(i_origin, j_origin, i_next, j_next, k, n, a, sums, tsum, num_map, mask, path)
def calc_sums(k, n, a):
sums = [0] * k
for i in range(k):
for j in range(n[i]):
sums[i] = sums[i] + a[i][j]
asum = 0
for i in range(k):
asum = asum + sums[i]
return asum, sums
if __name__ == "__main__":
main()
``` | instruction | 0 | 97,076 | 12 | 194,152 |
Yes | output | 1 | 97,076 | 12 | 194,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers.
There are k boxes numbered from 1 to k. The i-th box contains n_i integer numbers. The integers can be negative. All of the integers are distinct.
Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, k integers in total. Then he will insert the chosen numbers β one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box.
Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be?
Input
The first line contains a single integer k (1 β€ k β€ 15), the number of boxes.
The i-th of the next k lines first contains a single integer n_i (1 β€ n_i β€ 5 000), the number of integers in box i. Then the same line contains n_i integers a_{i,1}, β¦, a_{i,n_i} (|a_{i,j}| β€ 10^9), the integers in the i-th box.
It is guaranteed that all a_{i,j} are distinct.
Output
If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output k lines. The i-th of these lines should contain two integers c_i and p_i. This means that Ujan should pick the integer c_i from the i-th box and place it in the p_i-th box afterwards.
If there are multiple solutions, output any of those.
You can print each letter in any case (upper or lower).
Examples
Input
4
3 1 7 4
2 3 2
2 8 5
1 10
Output
Yes
7 2
2 3
5 1
10 4
Input
2
2 3 -2
2 -1 5
Output
No
Input
2
2 -10 10
2 0 -20
Output
Yes
-10 2
-20 1
Note
In the first sample, Ujan can put the number 7 in the 2nd box, the number 2 in the 3rd box, the number 5 in the 1st box and keep the number 10 in the same 4th box. Then the boxes will contain numbers \{1,5,4\}, \{3, 7\}, \{8,2\} and \{10\}. The sum in each box then is equal to 10.
In the second sample, it is not possible to pick and redistribute the numbers in the required way.
In the third sample, one can swap the numbers -20 and -10, making the sum in each box equal to -10.
Submitted Solution:
```
import sys
n = sys.stdin.readline()
n = int(n)
def get_graph(n):
graph = []
for _ in range(n):
entries = list(map(lambda x : int(x), sys.stdin.readline().split(" ")[1:]))
graph.append(entries)
return graph
#run is binary arr
def helper(target, buckets, reverse_bucket, sum_bucket, mem, mask, run):
if ~mask in mem:
return mem[mask] + run
main = None
curr = mask
count = len(buckets) - 1
while count >= 0:
if curr & 1 == 0:
main = count
break
curr = curr >> 1
count -= 1
if main is None:
return run
for x in buckets[main]:
new_run = set()
buckets_seen = set()
running_bucket = main
new_mask = mask
fucked = False
while True:
val = target - (sum_bucket[running_bucket] - x)
if val not in reverse_bucket:
break
new_bucket = reverse_bucket[val]
if new_bucket == running_bucket and x != val:
break
if (val, running_bucket) not in new_run and running_bucket not in buckets_seen:
if (mask >> (len(buckets)-1-new_bucket)) & 1 == 1:
fucked = True
x = val
new_run.add((val, running_bucket))
buckets_seen.add(running_bucket)
running_bucket = new_bucket
new_mask = new_mask | 2**(len(buckets)-1-new_bucket)
elif (val, running_bucket) not in new_run and running_bucket in buckets_seen:
break
elif (val, running_bucket) in new_run and running_bucket in buckets_seen:
mem[new_mask] = list(new_run)
if fucked:
return
run.extend(list(new_run))
return helper(target, buckets, reverse_bucket, sum_bucket, mem, new_mask, run)
def solve(n):
buckets = get_graph(n)
reverse_bucket = {}
sum_bucket = [0]* len(buckets)
total_sum = 0
for i, bucket in enumerate(buckets):
for x in bucket:
total_sum += x
sum_bucket[i] += x
reverse_bucket[x] = i
target = total_sum / len(buckets)
mem = {}
run = []
return helper(target, buckets, reverse_bucket, sum_bucket, mem, 0, run)
def result(n):
res = solve(n)
if res is None:
sys.stdout.write("No\n")
else:
sys.stdout.write("Yes\n")
for x, y in res:
x = int(x)
y += 1
stuff = " ".join([str(x), str(y), "\n"])
sys.stdout.write(stuff)
result(n)
``` | instruction | 0 | 97,077 | 12 | 194,154 |
No | output | 1 | 97,077 | 12 | 194,155 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers.
There are k boxes numbered from 1 to k. The i-th box contains n_i integer numbers. The integers can be negative. All of the integers are distinct.
Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, k integers in total. Then he will insert the chosen numbers β one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box.
Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be?
Input
The first line contains a single integer k (1 β€ k β€ 15), the number of boxes.
The i-th of the next k lines first contains a single integer n_i (1 β€ n_i β€ 5 000), the number of integers in box i. Then the same line contains n_i integers a_{i,1}, β¦, a_{i,n_i} (|a_{i,j}| β€ 10^9), the integers in the i-th box.
It is guaranteed that all a_{i,j} are distinct.
Output
If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output k lines. The i-th of these lines should contain two integers c_i and p_i. This means that Ujan should pick the integer c_i from the i-th box and place it in the p_i-th box afterwards.
If there are multiple solutions, output any of those.
You can print each letter in any case (upper or lower).
Examples
Input
4
3 1 7 4
2 3 2
2 8 5
1 10
Output
Yes
7 2
2 3
5 1
10 4
Input
2
2 3 -2
2 -1 5
Output
No
Input
2
2 -10 10
2 0 -20
Output
Yes
-10 2
-20 1
Note
In the first sample, Ujan can put the number 7 in the 2nd box, the number 2 in the 3rd box, the number 5 in the 1st box and keep the number 10 in the same 4th box. Then the boxes will contain numbers \{1,5,4\}, \{3, 7\}, \{8,2\} and \{10\}. The sum in each box then is equal to 10.
In the second sample, it is not possible to pick and redistribute the numbers in the required way.
In the third sample, one can swap the numbers -20 and -10, making the sum in each box equal to -10.
Submitted Solution:
```
def main():
k = int(input())
n = []
a = []
for i in range(k):
line = [int(x) for x in input().split()]
ni = line[0]
ai = []
n.append(ni)
a.append(ai)
for j in range(ni):
ai.append(line[1 + j])
answer, c, p = solve(k, n, a)
if answer:
print("Yes")
for i in range(k):
print(c[i], p[i] + 1)
else:
print("No")
def solve(k, n, a):
asum, sums = calc_sums(k, n, a)
if asum % k != 0:
return False, None, None
tsum = asum / k
num_map = build_num_map(k, n, a)
masks = [None]*(1 << k)
simple = [False]*(1 << k)
answer = [False]*(1 << k)
left = [0]*(1 << k)
right = [0]*(1 << k)
by_last_one = [[] for _ in range(k)]
for i in range(k):
for j in range(n[i]):
found, mask, path = find_cycle(i, j, i, j, k, n, a, sums, tsum, num_map, 0, [])
if found and not answer[mask]:
answer[mask] = True
masks[mask] = path
simple[mask] = True
by_last_one[calc_last_one(mask)].append(mask)
if answer[(1 << k) - 1]:
return build_answer(k, masks, left, right)
for mask_right in range(2, 1 << k):
if not simple[mask_right]:
continue
last_one = calc_last_one(mask_right)
zeroes_count = 0
last_zero = -1
u1 = 1
for u in range(last_one):
if (mask_right & u1) == 0:
zeroes_count += 1
last_zero = u
u1 *= 2
if zeroes_count == 0:
continue
if 0 * len(by_last_one[last_zero]) < (1 << zeroes_count):
for mask_left in by_last_one[last_zero]:
if (mask_left & mask_right) != 0:
continue
joint_mask = mask_left | mask_right
if not answer[joint_mask]:
answer[joint_mask] = True
left[joint_mask] = mask_left
right[joint_mask] = mask_right
by_last_one[last_one].append(joint_mask)
if joint_mask == ((1 << k) - 1):
return build_answer(k, masks, left, right)
else:
for mask_mask in range(1 << zeroes_count):
mask_left = 0
c1 = 1
u1 = 1
for u in range(last_zero + 1):
if (mask_right & u1) == 0:
if (mask_mask & c1) != 0:
mask_left = mask_left | u1
c1 *= 2
u1 *= 2
joint_mask = mask_left | mask_right
if answer[mask_left] and not answer[joint_mask]:
answer[joint_mask] = True
left[joint_mask] = mask_left
right[joint_mask] = mask_right
by_last_one[last_one].append(joint_mask)
if joint_mask == ((1 << k) - 1):
return build_answer(k, masks, left, right)
return False, None, None
def calc_last_one(x):
result = -1
while x > 0:
x = x >> 1
result = result + 1
return result
def build_answer(k, masks, left, right):
c = [-1] * k
p = [-1] * k
pos = (1 << k) - 1
while not masks[pos]:
for i, a, j in masks[right[pos]]:
c[i] = a
p[i] = j
pos = left[pos]
for i, a, j in masks[pos]:
c[i] = a
p[i] = j
return True, c, p
def build_num_map(k, n, a):
result = dict()
for i in range(k):
for j in range(n[i]):
result[a[i][j]] = (i, j)
return result
def find_cycle(i_origin, j_origin, i, j, k, n, a, sums, tsum, num_map, mask, path):
if (mask & (1 << i)) != 0:
if i == i_origin and j == j_origin:
return True, mask, path
else:
return False, None, None
mask = mask | (1 << i)
a_needed = tsum - (sums[i] - a[i][j])
if a_needed not in num_map:
return False, None, None
i_next, j_next = num_map[a_needed]
path.append((i_next, a[i_next][j_next], i))
return find_cycle(i_origin, j_origin, i_next, j_next, k, n, a, sums, tsum, num_map, mask, path)
def calc_sums(k, n, a):
sums = [0] * k
for i in range(k):
for j in range(n[i]):
sums[i] = sums[i] + a[i][j]
asum = 0
for i in range(k):
asum = asum + sums[i]
return asum, sums
if __name__ == "__main__":
main()
``` | instruction | 0 | 97,078 | 12 | 194,156 |
No | output | 1 | 97,078 | 12 | 194,157 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers.
There are k boxes numbered from 1 to k. The i-th box contains n_i integer numbers. The integers can be negative. All of the integers are distinct.
Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, k integers in total. Then he will insert the chosen numbers β one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box.
Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be?
Input
The first line contains a single integer k (1 β€ k β€ 15), the number of boxes.
The i-th of the next k lines first contains a single integer n_i (1 β€ n_i β€ 5 000), the number of integers in box i. Then the same line contains n_i integers a_{i,1}, β¦, a_{i,n_i} (|a_{i,j}| β€ 10^9), the integers in the i-th box.
It is guaranteed that all a_{i,j} are distinct.
Output
If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output k lines. The i-th of these lines should contain two integers c_i and p_i. This means that Ujan should pick the integer c_i from the i-th box and place it in the p_i-th box afterwards.
If there are multiple solutions, output any of those.
You can print each letter in any case (upper or lower).
Examples
Input
4
3 1 7 4
2 3 2
2 8 5
1 10
Output
Yes
7 2
2 3
5 1
10 4
Input
2
2 3 -2
2 -1 5
Output
No
Input
2
2 -10 10
2 0 -20
Output
Yes
-10 2
-20 1
Note
In the first sample, Ujan can put the number 7 in the 2nd box, the number 2 in the 3rd box, the number 5 in the 1st box and keep the number 10 in the same 4th box. Then the boxes will contain numbers \{1,5,4\}, \{3, 7\}, \{8,2\} and \{10\}. The sum in each box then is equal to 10.
In the second sample, it is not possible to pick and redistribute the numbers in the required way.
In the third sample, one can swap the numbers -20 and -10, making the sum in each box equal to -10.
Submitted Solution:
```
import sys
n = sys.stdin.readline()
n = int(n)
def get_graph(n):
graph = []
for _ in range(n):
entries = list(map(lambda x : int(x), sys.stdin.readline().split(" ")[1:]))
graph.append(entries)
return graph
#run is binary arr
def helper(target, buckets, reverse_bucket, sum_bucket, mem, mask, run):
if ~mask in mem:
return mem[mask] + run
main = None
curr = mask
count = len(buckets) - 1
while count >= 0:
if curr & 1 == 0:
main = count
break
curr = curr >> 1
count -= 1
if main is None:
return run
for x in buckets[main]:
new_run = set()
buckets_seen = set()
running_bucket = main
new_mask = mask
fucked = False
while True:
val = target - (sum_bucket[running_bucket] - x)
if val not in reverse_bucket:
break
new_bucket = reverse_bucket[val]
if new_bucket == running_bucket and x != val:
break
if (val, running_bucket) not in new_run and running_bucket not in buckets_seen:
if (mask >> (len(buckets)-1-new_bucket)) & 1 == 1:
fucked = True
x = val
new_run.add((val, running_bucket))
buckets_seen.add(running_bucket)
running_bucket = new_bucket
new_mask = new_mask | 2**(len(buckets)-1-new_bucket)
elif (val, running_bucket) not in new_run and running_bucket in buckets_seen:
break
elif (val, running_bucket) in new_run and running_bucket in buckets_seen:
mem[new_mask] = list(new_run)
if fucked:
break
run.extend(list(new_run))
return helper(target, buckets, reverse_bucket, sum_bucket, mem, new_mask, run)
def solve(n):
buckets = get_graph(n)
reverse_bucket = {}
sum_bucket = [0]* len(buckets)
total_sum = 0
for i, bucket in enumerate(buckets):
for x in bucket:
total_sum += x
sum_bucket[i] += x
reverse_bucket[x] = i
target = total_sum / len(buckets)
mem = {}
run = []
return helper(target, buckets, reverse_bucket, sum_bucket, mem, 0, run), reverse_bucket
def result(n):
res, reverse_bucket = solve(n)
if res is None:
sys.stdout.write("No\n")
else:
res = sorted(res, key = lambda x : reverse_bucket[x[0]])
sys.stdout.write("Yes\n")
for x, y in res:
x = int(x)
y += 1
stuff = " ".join([str(x), str(y), "\n"])
sys.stdout.write(stuff)
result(n)
``` | instruction | 0 | 97,079 | 12 | 194,158 |
No | output | 1 | 97,079 | 12 | 194,159 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to reorder the numbers.
There are k boxes numbered from 1 to k. The i-th box contains n_i integer numbers. The integers can be negative. All of the integers are distinct.
Ujan is lazy, so he will do the following reordering of the numbers exactly once. He will pick a single integer from each of the boxes, k integers in total. Then he will insert the chosen numbers β one integer in each of the boxes, so that the number of integers in each box is the same as in the beginning. Note that he may also insert an integer he picked from a box back into the same box.
Ujan will be happy if the sum of the integers in each box is the same. Can he achieve this and make the boxes perfectly balanced, like all things should be?
Input
The first line contains a single integer k (1 β€ k β€ 15), the number of boxes.
The i-th of the next k lines first contains a single integer n_i (1 β€ n_i β€ 5 000), the number of integers in box i. Then the same line contains n_i integers a_{i,1}, β¦, a_{i,n_i} (|a_{i,j}| β€ 10^9), the integers in the i-th box.
It is guaranteed that all a_{i,j} are distinct.
Output
If Ujan cannot achieve his goal, output "No" in a single line. Otherwise in the first line output "Yes", and then output k lines. The i-th of these lines should contain two integers c_i and p_i. This means that Ujan should pick the integer c_i from the i-th box and place it in the p_i-th box afterwards.
If there are multiple solutions, output any of those.
You can print each letter in any case (upper or lower).
Examples
Input
4
3 1 7 4
2 3 2
2 8 5
1 10
Output
Yes
7 2
2 3
5 1
10 4
Input
2
2 3 -2
2 -1 5
Output
No
Input
2
2 -10 10
2 0 -20
Output
Yes
-10 2
-20 1
Note
In the first sample, Ujan can put the number 7 in the 2nd box, the number 2 in the 3rd box, the number 5 in the 1st box and keep the number 10 in the same 4th box. Then the boxes will contain numbers \{1,5,4\}, \{3, 7\}, \{8,2\} and \{10\}. The sum in each box then is equal to 10.
In the second sample, it is not possible to pick and redistribute the numbers in the required way.
In the third sample, one can swap the numbers -20 and -10, making the sum in each box equal to -10.
Submitted Solution:
```
def main():
k = int(input())
n = []
a = []
for i in range(k):
line = [int(x) for x in input().split()]
ni = line[0]
ai = []
n.append(ni)
a.append(ai)
for j in range(ni):
ai.append(line[1 + j])
answer, c, p = solve(k, n, a)
if answer:
print("Yes")
for i in range(k):
print(c[i], p[i] + 1)
else:
print("No")
def solve(k, n, a):
asum, sums = calc_sums(k, n, a)
if asum % k != 0:
return False, None, None
tsum = asum / k
num_map = build_num_map(k, n, a)
masks = [None]*(1 << k)
simple = [False]*(1 << k)
answer = [False]*(1 << k)
left = [0]*(1 << k)
right = [0]*(1 << k)
by_last_one = [[] for _ in range(k)]
for i in range(k):
for j in range(n[i]):
found, mask, path = find_cycle(i, j, i, j, k, n, a, sums, tsum, num_map, 0, [])
if found:
answer[mask] = True
masks[mask] = path
simple[mask] = True
by_last_one[calc_last_one(mask)].append(mask)
if answer[(1 << k) - 1]:
return build_answer(k, masks, left, right)
for mask_right in range(2, 1 << k):
if not simple[mask_right]:
continue
last_one = calc_last_one(mask_right)
zeroes_count = 0
last_zero = -1
u1 = 1
for u in range(last_one):
if (mask_right & u1) == 0:
zeroes_count += 1
last_zero = u
u1 *= 2
if zeroes_count == 0:
continue
if len(by_last_one[last_zero]) < (1 << zeroes_count):
for mask_left in by_last_one[last_zero]:
if (mask_left & mask_right) != 0:
continue
joint_mask = mask_left | mask_right
if not answer[joint_mask]:
answer[joint_mask] = True
left[joint_mask] = mask_left
right[joint_mask] = mask_right
by_last_one[last_one].append(joint_mask)
if joint_mask == ((1 << k) - 1):
return build_answer(k, masks, left, right)
else:
for mask_mask in range(1 << zeroes_count):
mask_left = 0
c1 = 1
u1 = 1
for u in range(last_zero + 1):
if (mask_right & u1) == 0:
if (mask_mask & c1) != 0:
mask_left = mask_left | u1
c1 *= 2
u1 *= 2
joint_mask = mask_left | mask_right
if answer[mask_left] and not answer[joint_mask]:
answer[joint_mask] = True
left[joint_mask] = mask_left
right[joint_mask] = mask_right
by_last_one[last_one].append(joint_mask)
if joint_mask == ((1 << k) - 1):
return build_answer(k, masks, left, right)
return False, None, None
def calc_last_one(x):
result = -1
while x > 0:
x = x >> 1
result = result + 1
return result
def build_answer(k, masks, left, right):
c = [-1] * k
p = [-1] * k
pos = (1 << k) - 1
while not masks[pos]:
for i, a, j in masks[right[pos]]:
c[i] = a
p[i] = j
pos = left[pos]
for i, a, j in masks[pos]:
c[i] = a
p[i] = j
return True, c, p
def build_num_map(k, n, a):
result = dict()
for i in range(k):
for j in range(n[i]):
result[a[i][j]] = (i, j)
return result
def find_cycle(i_origin, j_origin, i, j, k, n, a, sums, tsum, num_map, mask, path):
if (mask & (1 << i)) != 0:
if i == i_origin and j == j_origin:
return True, mask, path
else:
return False, None, None
mask = mask | (1 << i)
a_needed = tsum - (sums[i] - a[i][j])
if a_needed not in num_map:
return False, None, None
i_next, j_next = num_map[a_needed]
path.append((i_next, a[i_next][j_next], i))
return find_cycle(i_origin, j_origin, i_next, j_next, k, n, a, sums, tsum, num_map, mask, path)
def calc_sums(k, n, a):
sums = [0] * k
for i in range(k):
for j in range(n[i]):
sums[i] = sums[i] + a[i][j]
asum = 0
for i in range(k):
asum = asum + sums[i]
return asum, sums
if __name__ == "__main__":
main()
``` | instruction | 0 | 97,080 | 12 | 194,160 |
No | output | 1 | 97,080 | 12 | 194,161 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,149 | 12 | 194,298 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
for _ in range(int(input())):
n,k=map(int,input().split())
l1,r1=map(int,input().split())
l2,r2=map(int,input().split())
if l1>l2:
l1,r1,l2,r2=l2,r2,l1,r1
if l2>r1:
ans=float("inf")
for i in range(1,n+1):
temp=i*(l2-r1)
limit=(r2-l1)*i
if k<=limit:
temp+=k
ans=min(ans,temp)
else:
temp+=limit
K=k-limit
temp+=2*K
ans=min(ans,temp)
print(ans)
else:
count=n*(min(r1,r2)-l2)
res=0
if count>k:
print(res)
continue
b=max(r2,r1)-min(l2,l1)-(min(r1,r2)-l2)
for i in range(n):
if k-count<=b:
res+=k-count
break
else:
res+=b
count+=b
else:
res+=(k-count)*2
print(res)
``` | output | 1 | 97,149 | 12 | 194,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,150 | 12 | 194,300 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n,k = list(map(int,input().split()))
l1,r1 = list(map(int,input().split()))
l2,r2 = list(map(int,input().split()))
#make it same
if r1>r2:
l1, r1, l2, r2 = l2, r2, l1, r1
#reduce k by overlap
overlap = max(0, r1-max(l1,l2))
k -= overlap * n
#check border condition
if k<=0:
print(0)
continue
touch_cost = max(0, max(l1,l2)-min(r1,r2))
overlap_cost = max(r2,r1) - min(l1,l2) - overlap
overlap_gain = max(r1,r2) - min(l1,l2)
#print(touch_cost, overlap_cost, overlap_gain)
#make one touch and double extend
ans = 2*k + touch_cost
#print(ans)
for i in range(1, n+1):
poss = touch_cost * i
if i * overlap_cost>=k:
poss += k
else:
poss += 2*k - overlap_cost*i
#print(i, poss)
ans = min(poss, ans)
print(ans)
``` | output | 1 | 97,150 | 12 | 194,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,151 | 12 | 194,302 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
for _ in range(int(input())):
n,k=map(int,input().split())
l1,r1=map(int,input().split())
l2,r2=map(int,input().split())
over=min(r1,r2)-max(l1,l2)
moves=0
#Case1: Already overlapped
if (l1,r1)==(l2,r2):
if (r1-l1)*n>=k:
print(0)
else:
k-=(r1-l1)*n
print(2*k)
elif over>0:
over*=n
if over>=k:
print(0)
else:
o=max(r1,r2)-min(l1,l2)-over//n
if over+o*n>=k:
print(k-over)
else:
moves+=o*n
over+=o*n
print(moves+(k-over)*2)
#Case 2: No overlap(Here the fun begins)
else:
d=-1*over
o=max(r1,r2)-min(l1,l2)
x=k//o
k=k%o
if n>x:
moves+=(x)*(d+o)
if d<k or x==0:
moves+=d+k
else:
moves+=2*k
else:
moves+=n*(d+o)+((x-n)*o+k)*2
print(moves)
``` | output | 1 | 97,151 | 12 | 194,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,152 | 12 | 194,304 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
l1, r1 = map(int, input().split())
l2, r2 = map(int, input().split())
if l1 > l2:
l1, r1, l2, r2 = l2, r2, l1, r1
ans = 10**18
if r1 < l2:
shared = 0
initial_cost = l2 - r1
max_len = r2 - l1
else:
shared = min(r1, r2) - l2
initial_cost = 0
max_len = max(r1, r2) - l1 - shared
k = max(0, k - shared * n)
if k == 0:
ans = 0
# Fill with i-area
for i in range(n + 1):
if i == 0 and initial_cost > 0:
continue
# Connect i
cost = i * initial_cost
rest = max(0, k - i * max_len)
ans = min(ans, cost + (k - rest) + rest * 2)
print(ans)
``` | output | 1 | 97,152 | 12 | 194,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,153 | 12 | 194,306 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
for tt in range(int(input())):
n,z = input().split(' ')
Al,Ar = input().split(' ')
Bl,Br = input().split(' ')
n,z,Al,Ar,Bl,Br = int(n),int(z),int(Al),int(Ar),int(Bl),int(Br)
if Al>Bl:
Al,Ar,Bl,Br = Bl,Br,Al,Ar
if Ar<Bl:
blank = Bl-Ar
get = Br-Al
need = blank+get
if z<get:
print(blank+z)
elif z<=n*get:
print(int(z/get)*need+min(z%get,blank)+z%get)
else:
print(n*need+(z-n*get)*2)
else:
get = max(Br,Ar) - Al
init = n*(min(Br,Ar)-Bl)
if z<=init:
print(0)
elif z<=n*get:
print(z-init)
else:
print(n*get-init + 2*(z-n*get))
``` | output | 1 | 97,153 | 12 | 194,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,154 | 12 | 194,308 |
Tags: brute force, greedy, implementation, 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()]
l1,r1=[int(x) for x in input().split()]
l2,r2=[int(x) for x in input().split()]
its=min(r1,r2)-max(l1,l2) #intersect
if its>=0: #intersect
k-=n*its
if k<=0:ans=0
else:
aoc=max(r1,r2)-min(l1,l2) #add-one-capacity
if n*(aoc-its)>=k:
ans=k
else:
ans=n*(aoc-its)
k-=n*(aoc-its)
ans+=k*2 #every k requires 2 steps
print(ans)
else: #no intersection
gap=-its
aoc=max(r1,r2)-min(l1,l2) #add-one-capacity, only after gap is filled up
minn=float('inf')
for i in range(1,n+1): #fill for i segments
total=gap*i
if aoc*i>=k:total+=k
else:
total+=aoc*i+(k-aoc*i)*2
minn=min(minn,total)
print(minn)
``` | output | 1 | 97,154 | 12 | 194,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,155 | 12 | 194,310 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
#don't
n,k = map(int,stdin.readline().split() )
l1,r1 = map(int,stdin.readline().split())
l2,r2 = map(int,stdin.readline().split())
#don't
if l1 >= r2 or l2 >= r1:
ans = float("inf")
for usenum in range(1,n+1):
if max(abs(l1-r2),abs(l2-r1))*usenum >= k:
ans = min(ans , min(abs(l1-r2),abs(l2-r1))*usenum + k)
else:
rem = k - max(abs(l1-r2),abs(l2-r1))*usenum
ans = min(ans , min(abs(l1-r2),abs(l2-r1))*usenum + max(abs(l1-r2),abs(l2-r1))*usenum + rem*2)
print (ans)
else:
ans = float("inf")
over = min(r1-l1 , r2-l2 , r1-l2 , r2-l1)
usenum = n
if over * usenum >= k:
print (0)
continue
k -= over * usenum
useable = max(r1-l1 , r2-l2 , r1-l2 , r2-l1) - over
if useable * usenum >= k:
print (k)
continue
else:
rem = k - useable * usenum
print (useable * usenum + rem*2)
``` | output | 1 | 97,155 | 12 | 194,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,156 | 12 | 194,312 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
# e053d9db8efe450d25f052c2a1e0f08131c83762143ed09d442439e0eaa5681d
import sys
input = sys.stdin.buffer.readline
def print(val):
sys.stdout.write(str(val) + '\n')
def prog():
for _ in range(int(input())):
n,k = map(int,input().split())
l1,r1 = map(int,input().split())
l2,r2 = map(int,input().split())
if(l1 > l2):
p1 = l1
p2 = r1
l1 = l2
r1 = r2
l2 = p1
r2 = p2
total = 0
moves = 0
if (r1 >= l2):
intersection = min(r1,r2) - max(l1,l2)
l3 = min(l1,l2)
r3 = max(r1,r2)
leftover = r3 - l3 - intersection
k -= intersection*n
if (k <= 0):
print(0)
elif(k - leftover*n <= 0):
print(k)
else:
k -= leftover*n
moves += leftover*n
moves += k*2
k = 0
print(moves)
else:
l3 = l1
r3 = r2
a = l2 - r1
if (r3 - l3)*n >= k:
if (k <= r3 - l3):
print(a + k)
continue
else:
k -= r3 - l3
moves += a + r3 - l3
if (r3 -l3 + a >= 2*(r3-l3)):
moves += 2*k
else:
moves += (r3-l3+a)*(k//(r3-l3))
moves += min(k %(r3-l3) + a , 2*(k % (r3-l3)))
print(moves)
else:
k -= (r3 - l3)*n
moves += (r3-l3 + a)*n
moves += k*2
print(moves)
prog()
``` | output | 1 | 97,156 | 12 | 194,313 |
Provide tags and a correct Python 2 solution for this coding contest problem.
You are given two lists of segments [al_1, ar_1], [al_2, ar_2], ..., [al_n, ar_n] and [bl_1, br_1], [bl_2, br_2], ..., [bl_n, br_n].
Initially, all segments [al_i, ar_i] are equal to [l_1, r_1] and all segments [bl_i, br_i] are equal to [l_2, r_2].
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1. In other words, suppose you've chosen segment [x, y] then you can transform it either into [x - 1, y] or into [x, y + 1].
Let's define a total intersection I as the sum of lengths of intersections of the corresponding pairs of segments, i.e. β_{i=1}^{n}{intersection_length([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 0 and length of a segment [x, y] is equal to y - x.
What is the minimum number of steps you need to make I greater or equal to k?
Input
The first line contains the single integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ 10^9) β the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l_1 and r_1 (1 β€ l_1 β€ r_1 β€ 10^9) β the segment all [al_i, ar_i] are equal to initially.
The third line of each test case contains two integers l_2 and r_2 (1 β€ l_2 β€ r_2 β€ 10^9) β the segment all [bl_i, br_i] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2 β
10^5.
Output
Print t integers β one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k.
Example
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8
Output
7
2000000000
0
Note
In the first test case, we can achieve total intersection 5, for example, using next strategy:
* make [al_1, ar_1] from [1, 2] to [1, 4] in 2 steps;
* make [al_2, ar_2] from [1, 2] to [1, 3] in 1 step;
* make [bl_1, br_1] from [3, 4] to [1, 4] in 2 steps;
* make [bl_2, br_2] from [3, 4] to [1, 4] in 2 steps.
In result, I = intersection_length([al_1, ar_1], [bl_1, br_1]) + intersection_length([al_2, ar_2], [bl_2, br_2]) + \\\ + intersection_length([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5
In the second test case, we can make [al_1, ar_1] = [0, 1000000000] in 1000000000 steps and [bl_1, br_1] = [0, 1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10 > 3, so we don't need to do any steps. | instruction | 0 | 97,157 | 12 | 194,314 |
Tags: brute force, greedy, implementation, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
pr=stdout.write
import heapq
raw_input = stdin.readline
def ni():
return int(raw_input())
def li():
return list(map(int,raw_input().split()))
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return (map(int,stdin.read().split()))
range = xrange # not for python 3.0+
for t in range(ni()):
n,k=li()
l1,r1=li()
l2,r2=li()
df=0
if not ( (l1<=r2 and l1>=l2) or (r1>=l2 and r1<=r2) or (l2<=r1 and l2>=l1) or (r2>=l1 and r2<=r1)):
df=min(int(abs(r2-l1)),int(abs(l2-r1)))
else:
x=[l1,r1,l2,r2]
x.sort()
ln=x[2]-x[1]
ans=min(k,ln*n)
k-=ans
ans=0
mx=max(l1,l2,r1,r2)-min(l1,l2,r1,r2)-ln
ans+=min(k,mx*n)
k-=min(k,mx*n)
pn(ans+(2*k))
continue
mx=max(l1,l2,r1,r2)-min(l1,l2,r1,r2)
ans=df
if k<mx:
print ans+k
continue
ans+=mx
k-=mx
if mx:
cst=(df+mx)/float(mx)
else:
cst=10**12
if cst<2:
for i in range(n-1):
if k<mx:
ans+=min(2*k,k+df)
k=0
else:
ans+=mx+df
k-=mx
ans+=2*k
pn(ans)
``` | output | 1 | 97,157 | 12 | 194,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,199 | 12 | 194,398 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
res = [n]
for i in range(1,n):
res.append(i)
print(*res)
``` | output | 1 | 97,199 | 12 | 194,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,200 | 12 | 194,400 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
def lip(): return list(map(int,input().split()))
def splip(): return map(int,input().split())
def intip(): return int(input())
for _ in range(intip()):
n = intip()
l = [i for i in range(1,n+1)]
l = l[::-1]
mid = n//2
if n==2:
print(*l)
elif n%2!=0:
l[mid] , l[mid-1] = l[mid-1],l[mid]
print(*l)
else:
l[mid+1] , l[mid] = l[mid],l[mid+1]
print(*l)
``` | output | 1 | 97,200 | 12 | 194,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,201 | 12 | 194,402 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
t= int (input())
if t:
for i in range(t):
n= int(input())
if n:
print(n, *range(1,n))
``` | output | 1 | 97,201 | 12 | 194,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,202 | 12 | 194,404 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
N = int(input())
for _ in range(N):
n = int(input())
a = []
for i in range(0,n):
a.insert(0,i+1)
if n % 2==1:
a[0] = a[n // 2]
a[n // 2] = n
print(*a)
``` | output | 1 | 97,202 | 12 | 194,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,203 | 12 | 194,406 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
for _ in range(int(input())):
N = int(input())
for i in range(2,N+1):
print(i, end = " ")
print(1)
``` | output | 1 | 97,203 | 12 | 194,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,204 | 12 | 194,408 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
if n % 2 == 0:
for i in range(n, 0, -1):
print(i, end=' ')
print()
else:
for i in range(n, n // 2 + 1, -1):
print(i, end=' ')
for i in range(1, n // 2 + 2):
print(i, end=' ')
print()
``` | output | 1 | 97,204 | 12 | 194,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,205 | 12 | 194,410 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
def solve(n):
a=[0]*n
a[0]=n
for i in range(1,n):
a[i]=i
return a
t=int(input())
ans=[]
while t>0:
n=int(input(''))
ans.append(solve(n))
t-=1
for i in ans:
for j in i:
print(j,end=' ')
print()
``` | output | 1 | 97,205 | 12 | 194,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4 | instruction | 0 | 97,206 | 12 | 194,412 |
Tags: constructive algorithms, probabilities
Correct Solution:
```
t=int(input())
for q in range(t):
n=int(input())
ch=str(n)+" "
for i in range(1,n):
ch+=str(i)+" "
print(ch)
``` | output | 1 | 97,206 | 12 | 194,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
for i in range(2,n+1):
print(i,end=" ")
print(1)
``` | instruction | 0 | 97,207 | 12 | 194,414 |
Yes | output | 1 | 97,207 | 12 | 194,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
t=int(input())
for _ in range(t):
n=int(input())
oneLineArrayPrint([n]+list(range(1,n)))
``` | instruction | 0 | 97,208 | 12 | 194,416 |
Yes | output | 1 | 97,208 | 12 | 194,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
t = int(input())
for i in range(t):
n = int(input())
a = []
while n>0:
a.append(n)
n-=1
if len(a)%2 == 0:
print(" ".join(map(str,a)))
else:
a1 = a[len(a)//2]
a.remove(a1)
a.append(a1)
print(" ".join(map(str,a)))
``` | instruction | 0 | 97,209 | 12 | 194,418 |
Yes | output | 1 | 97,209 | 12 | 194,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
import random
t = int(input())
while t:
n = int(input())
res= [n]
for i in range(1,n):
res.append(i)
print(" ".join(map(str,res)))
t-=1
``` | instruction | 0 | 97,210 | 12 | 194,420 |
Yes | output | 1 | 97,210 | 12 | 194,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
t = int(input())
for i in range (t):
n = int(input())
p = []
for j in range (n):
p.append(n)
n -= 1
print(*p)
``` | instruction | 0 | 97,211 | 12 | 194,422 |
No | output | 1 | 97,211 | 12 | 194,423 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
for i in range(int(input())):
n = int(input())
a = []
for i in range(2,n+1,2):
a.append(i)
for i in range(1,n+1,2):
a.append(i)
print(*a)
``` | instruction | 0 | 97,212 | 12 | 194,424 |
No | output | 1 | 97,212 | 12 | 194,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
for i in range(int(input())):
n=int(input())
for j in range(n):
if j==n-1:
print(1)
else:
print(n-j,end=" ")
print()
``` | instruction | 0 | 97,213 | 12 | 194,426 |
No | output | 1 | 97,213 | 12 | 194,427 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given one integer n (n > 1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2, 3, 1, 5, 4] is a permutation of length 5, but [1, 2, 2] is not a permutation (2 appears twice in the array) and [1, 3, 4] is also not a permutation (n = 3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Input
The first line of the input contains one integer t (1 β€ t β€ 100) β the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2 β€ n β€ 100) β the length of the permutation you have to find.
Output
For each test case, print n distinct integers p_1, p_2, β¦, p_n β a permutation that there is no index i (1 β€ i β€ n) such that p_i = i (so, for all i from 1 to n the condition p_i β i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n > 1.
Example
Input
2
2
5
Output
2 1
2 1 5 3 4
Submitted Solution:
```
import sys
# problem a
# template by:
# https://github.com/rajatg98
'''input
'''
import sys
import math
import bisect
from sys import stdin,stdout
from math import gcd,floor,sqrt,log
from collections import defaultdict as dd
from bisect import bisect_left as bl,bisect_right as br
sys.setrecursionlimit(100000000)
inp =lambda: int(input())
strng =lambda: input().strip()
jn =lambda x,l: x.join(map(str,l))
strl =lambda: list(input().strip())
mul =lambda: map(int,input().strip().split())
mulf =lambda: map(float,input().strip().split())
seq =lambda: list(map(int,input().strip().split()))
ceil =lambda x: int(x) if(x==int(x)) else int(x)+1
ceildiv=lambda x,d: x//d if(x%d==0) else x//d+1
flush =lambda: stdout.flush()
stdstr =lambda: stdin.readline()
stdint =lambda: int(stdin.readline())
stdpr =lambda x: stdout.write(str(x))
mod=1000000007
#main code
def solve():
n = inp()
print([n] + list(range(1, n)))
def main():
tests = inp()
for _ in range(tests):
solve()
if __name__ == "__main__":
main()
``` | instruction | 0 | 97,214 | 12 | 194,428 |
No | output | 1 | 97,214 | 12 | 194,429 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,361 | 12 | 194,722 |
Tags: greedy, hashing, implementation
Correct Solution:
```
(m, n) = map(int, input().split(' '))
B = []
for i in range(0,m):
row = [int(x) for x in input().split(' ')]
B.append(row)
B_ = []
for j in range(0,n):
col = [B[i][j] for i in range(0,m)]
B_.append(col)
for row in B:
if 1 in row:
if 0 in row:
for j in range(0,n):
col = [B[i][j] for i in range(0,m)]
if (row[j]==1 and 0 in col):
print("NO")
exit()
for col in B_:
if 1 in col:
if 0 in col:
for i in range(0,m):
row = [B[i][j] for j in range(0,n)]
if (col[i]==1 and 0 in row):
print("NO")
exit()
only1 = False
only0 = False
for row in B:
if 1 not in row:
only0 = True
if 0 not in row:
only1 = True
if only1 and only0:
print("NO")
exit()
only1 = False
only0 = False
for col in B_:
if 1 not in col:
only0 = True
if 0 not in col:
only1 = True
if only1 and only0:
print("NO")
exit()
A = [[0 for col in range(0,n)] for row in range(0,m)]
R = []
for i in range(0,m):
flg = 1
for j in range(0,n):
if B[i][j] == 0:
flg = 0
break
if flg == 1:
R.append(i)
C = []
for j in range(0,n):
flg = 1
for i in range(0,m):
if B[i][j] == 0:
flg = 0
break
if flg == 1:
C.append(j)
for r in R:
for c in C:
A[r][c] = 1
print("YES")
for i in range(0,m):
s = ""+str(A[i][0])
for j in range(1,n):
s += " "+str(A[i][j])
print(s)
``` | output | 1 | 97,361 | 12 | 194,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,362 | 12 | 194,724 |
Tags: greedy, hashing, implementation
Correct Solution:
```
# cook your dish here
m,n=map(int,input().split())
l=[]
for i in range(m) :
l.append(list(map(int,input().split())))
r=[]
c=[]
f=0
for i in range(m) :
for j in range(n) :
if l[i][j]==1 :
x=1
for k in range(m) :
if l[k][j]==0 :
x=0
break
if x==1 :
c.append(j)
for k in range(n) :
if l[i][k]==0 :
x=0
break
if x==1 :
r.append(i)
else :
for k in range(n) :
if l[i][k]==0 :
x=1
break
if x==0 :
r.append(i)
else :
f=1
break
if f==1 or (len(r)==0 and len(c)!=0) or (len(c)==0 and len(r)!=0):
print("NO")
else :
print("YES")
for i in range(m) :
for j in range(n) :
if i in r and j in c :
print(1,end=" ")
else :
print(0,end=" ")
print()
``` | output | 1 | 97,362 | 12 | 194,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,363 | 12 | 194,726 |
Tags: greedy, hashing, implementation
Correct Solution:
```
n,m= map(int,input().split())
b=[]
a=[m*[1] for _ in range(n)]
for i in range(n):
b+=[list(map(int,input().split()))]
for k in range(m):
if b[i][k]==0:
a[i]=m*[0]
for j in range(n):
a[j][k]=0
stop=0
i=0
while not stop and i<n:
for j in range(m):
s=0
for k in range(n):
s+=a[k][j]
if b[i][j]!=min(1,s+sum(a[i])):
stop=1
break
i+=1
if stop:
print("NO")
else:
print("YES")
for i in range(n):
print(*a[i])
``` | output | 1 | 97,363 | 12 | 194,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,364 | 12 | 194,728 |
Tags: greedy, hashing, implementation
Correct Solution:
```
a, b = map(int, input().split(' '))
array = [[1] * b for i in range(a)]
orig = [list(map(int, input().split(' '))) for i in range(a)]
matrix = []
for i in orig:
matrix.append(i[:])
row0 = []
col0 = []
for i in range(a):
for j in range(b):
if matrix[i][j] == 0:
row0.append(i)
col0.append(j)
row0 = list(set(row0))
col0 = list(set(col0))
for i in row0:
matrix[i] = [0] * b
for ele in col0:
for i in range(a):
matrix[i][ele] = 0
match = [[0] * b for i in range(a)]
for i in range(len(matrix)):
if 1 in matrix[i]:
match[i] = [1] * len(matrix[0])
jlist = []
for i in range(a):
for j in range(b):
if matrix[i][j] == 1:
jlist.append(j)
for i in jlist:
for bad in range(len(match)):
match[bad][i] = 1
if match == orig:
print("YES")
for i in matrix:
print(' '.join([str(j) for j in i]))
else:
print("NO")
``` | output | 1 | 97,364 | 12 | 194,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,365 | 12 | 194,730 |
Tags: greedy, hashing, implementation
Correct Solution:
```
m, n = [int(s) for s in input().split(' ')]
B = [[] for _ in range(m)]
A = [[1 for _ in range(n)] for _ in range(m)]
ARow = [n for _ in range(m)]
ACol = [m for _ in range(n)]
for i in range(m):
B[i] = [int(s) for s in input().split(' ')]
for j in range(n):
if B[i][j] == 0:
for x in range(m):
A[x][j] = 0
if ACol[j] > 0:
ARow[x] = max(ARow[x]-1, 0)
for y in range(n):
A[i][y] = 0
if ARow[i] > 0:
ACol[y] = max(ACol[y]-1, 0)
ARow[i] = 0
ACol[j] = 0
contradiction = False
for i in range(m):
if contradiction:
break
for j in range(n):
if B[i][j] == 1:
contradiction = ARow[i] == 0 and ACol[j] == 0
if contradiction:
break
if contradiction:
print('NO')
else:
print('YES')
for i in range(m):
print(' '.join(map(str, A[i])))
``` | output | 1 | 97,365 | 12 | 194,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,366 | 12 | 194,732 |
Tags: greedy, hashing, implementation
Correct Solution:
```
import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
def mi(s):
return map(int, s.strip().split())
def lmi(s):
return list(mi(s))
def tmi(s):
return tuple(mi(s))
def mf(f, s):
return map(f, s)
def lmf(f, s):
return list(mf(f, s))
def js(lst):
return " ".join(str(d) for d in lst)
def line():
return sys.stdin.readline().strip()
def linesp():
return line().split()
def iline():
return int(line())
def mat(n):
matr = []
for _ in range(n):
matr.append(linesp())
return matr
def mati(n):
mat = []
for _ in range(n):
mat.append(lmi(line()))
return mat
def pmat(mat):
for row in mat:
print(js(row))
def dist(x, y):
return ((x[0] - y[0])**2 + (x[1] - y[1])**2)**0.5
def fast_exp(x, n):
if n == 0:
return 1
elif n % 2 == 1:
return x * fast_exp(x, (n - 1) // 2)**2
else:
return fast_exp(x, n // 2)**2
def main(mat):
z_col = set()
z_row = set()
for i in range(len(mat)):
for j in range(len(mat[i])):
if mat[i][j] == 0:
# Implies that the row and col
# must both be 0.
z_row.add(i)
z_col.add(j)
ans = [[0 for _ in range(len(mat[i]))] for i in range(len(mat))]
row_done = set()
col_done = set()
pending_row = set()
pending_col = set()
for i in range(len(mat)):
for j in range(len(mat[i])):
if mat[i][j] == 1:
if i not in z_row and j not in z_col:
ans[i][j] = 1
row_done.add(i)
col_done.add(j)
if i in pending_row:
pending_row.remove(i)
if j in pending_col:
pending_col.remove(j)
elif i not in z_row and j in z_col:
# Current column must be a 0.
# So this row must have a one.
if i not in row_done:
# Need to do i.
pending_row.add(i)
elif i in z_row and j not in z_col:
if j not in col_done:
pending_col.add(j)
else:
print("NO")
return
# Try and complete the pending row.
for i in pending_row:
done = False
for j in range(len(mat[i])):
if j not in z_col:
ans[i][j] = 1
if j in pending_col:
pending_col.remove(j)
done = True
if not done:
print("NO")
return
for j in pending_col:
done = False
for i in range(len(mat)):
if i not in z_row:
ans[i][j] = 1
done = True
if not done:
print("NO")
return
print("YES")
pmat(ans)
def main(mat):
# Million times better solution.
A = [[1 for _ in range(len(mat[i]))] for i in range(len(mat))]
z_row = set()
z_col = set()
for i in range(len(mat)):
for j in range(len(mat[i])):
if mat[i][j] == 0:
# Make row/col 0.
z_row.add(i)
z_col.add(j)
for row in z_row:
for j in range(len(mat[0])):
A[row][j] = 0
for col in z_col:
for i in range(len(mat)):
A[i][col] = 0
# Use A to get mat.
B = [[1 for _ in range(len(mat[i]))] for i in range(len(mat))]
for i in range(len(A)):
for j in range(len(A[i])):
B[i][j] = 0
for k in range(len(A)):
B[i][j] |= A[k][j]
for k in range(len(A[0])):
B[i][j] |= A[i][k]
if B[i][j] != mat[i][j]:
print("NO")
return
print("YES")
pmat(A)
if __name__ == "__main__":
n, _ = mi(line())
mat = mati(n)
main(mat)
``` | output | 1 | 97,366 | 12 | 194,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,367 | 12 | 194,734 |
Tags: greedy, hashing, implementation
Correct Solution:
```
n,m=list(map(int,input().split()))
l=[]
for i in range(n):
l.append(list(map(int,input().split())))
a=[]
for i in range(n+1):
a.append([None for j in range(m+1)])
for i in range(n):
for j in range(m):
if l[i][j]==0:
for x in range(m):
a[i][x]=0
for x in range(n):
a[x][j]=0
else:
if a[i][j]!=0:
a[i][j]=1
for i in range(n):
c=0
for j in range(m):
if a[i][j]==1:
c=c+1
a[i][m]=c
for j in range(m):
c=0
for i in range(n):
if a[i][j]==1:
c=c+1
a[n][j]=c
b=[]
for i in range(n):
b.append([None for j in range(m)])
for i in range(n):
for j in range(m):
if a[i][j]==1:
for x in range(n):
b[x][j]=1
for x in range(m):
b[i][x]=1
else:
if b[i][j]!=1:
b[i][j]=0
if b==l:
print('YES')
for i in range(n):
for j in range(m-1):
print(a[i][j],end=' ')
print(a[i][m-1])
else:
print('NO')
``` | output | 1 | 97,367 | 12 | 194,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
<image> where <image> is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 β€ i β€ m) and column j (1 β€ j β€ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
<image>.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
Input
The first line contains two integer m and n (1 β€ m, n β€ 100), number of rows and number of columns of matrices respectively.
The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
Output
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
Examples
Input
2 2
1 0
0 0
Output
NO
Input
2 3
1 1 1
1 1 1
Output
YES
1 1 1
1 1 1
Input
2 3
0 1 0
1 1 1
Output
YES
0 0 0
0 1 0 | instruction | 0 | 97,368 | 12 | 194,736 |
Tags: greedy, hashing, implementation
Correct Solution:
```
m,n=list(map(int,input().split()))
grid = [list(map(int,input().split())) for i in range(m)]
def ANDrow(grid,i):
ans = 1
for j in range(n):
ans = ans and grid[i][j]
return ans
def ANDcol(grid,j):
ans = 1
for i in range(m):
ans = ans and grid[i][j]
return ans
gridA = []
for i in range(m):
temp= []
for j in range(n):
temp.append(0)
gridA.append(temp)
# gridA= [[0]*n]*m
flag = True
countR = 0
countC = 0
for i in range(m):
if not flag:
break
for j in range(n):
androw = ANDrow(grid[::],i)
countR+=androw
andcol = ANDcol(grid[::],j)
countC+=andcol
# print(grid[i][j],androw,andcol,grid[i][j] and androw and andcol)
gridA[i][j] = int(bool(grid[i][j]) and bool(androw) and bool(andcol))
# print(i,j,int(bool(grid[i][j]) and bool(androw) and bool(andcol)),gridA)
flag2 = (grid[i][j] and (not (androw or andcol)))
if flag2:
flag = False
break
if flag and not((countC==0) ^ (countR==0)):
print("YES")
for i in gridA:
print(*i)
else:
print("NO")
``` | output | 1 | 97,368 | 12 | 194,737 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $S$, find the smallest sub-array size (smallest window length) where the sum of the sub-array is greater than or equal to $S$. If there is not such sub-array, report 0.
Constraints
* $1 \leq N \leq 10^5$
* $1 \leq S \leq 10^9$
* $1 \leq a_i \leq 10^4$
Input
The input is given in the following format.
$N$ $S$
$a_1$ $a_2$ ... $a_N$
Output
Print the smallest sub-array size in a line.
Examples
Input
6 4
1 2 1 2 3 2
Output
2
Input
6 6
1 2 1 2 3 2
Output
3
Input
3 7
1 2 3
Output
0 | instruction | 0 | 97,805 | 12 | 195,610 |
"Correct Solution:
```
from queue import deque
INF = 2**31-1
n, s = map(int, input().split())
A = list(map(int, input().split()))
q = deque()
t = 0
ans = INF
for a in A:
t += a
q.append(a)
if t < s:
continue
while t >=s:
t -=q.popleft()
ans = min(ans,len(q)+1)
print(ans if ans < INF else 0)
``` | output | 1 | 97,805 | 12 | 195,611 |
Provide a correct Python 3 solution for this coding contest problem.
For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $S$, find the smallest sub-array size (smallest window length) where the sum of the sub-array is greater than or equal to $S$. If there is not such sub-array, report 0.
Constraints
* $1 \leq N \leq 10^5$
* $1 \leq S \leq 10^9$
* $1 \leq a_i \leq 10^4$
Input
The input is given in the following format.
$N$ $S$
$a_1$ $a_2$ ... $a_N$
Output
Print the smallest sub-array size in a line.
Examples
Input
6 4
1 2 1 2 3 2
Output
2
Input
6 6
1 2 1 2 3 2
Output
3
Input
3 7
1 2 3
Output
0 | instruction | 0 | 97,807 | 12 | 195,614 |
"Correct Solution:
```
from queue import deque
n, s = map(int, input().split())
q = deque()
t = 0
ans = 1e6
for a in map(int, input().split()):
t += a
q.append(a)
if t < s:
continue
while t >= s:
t -= q.popleft()
ans = min(ans, len(q) + 1)
print(ans if ans < 1e6 else 0)
``` | output | 1 | 97,807 | 12 | 195,615 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,890 | 12 | 195,780 |
Tags: implementation, math
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
k=1<<60
for i in range(n):
if i:
k=min(k,min(a[0],a[i])//i)
if i<n-1:
k=min(k,min(a[-1],a[i])//(n-1-i))
print(k)
``` | output | 1 | 97,890 | 12 | 195,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,891 | 12 | 195,782 |
Tags: implementation, math
Correct Solution:
```
def main():
n = int(input())
array = list(map(int,input().split()))
nums = []
for i in range(n):
nums.append((array[i],i+1))
k = float('inf')
for i in range(1,n):
diff = abs(i+1-1)
val = min(array[0],array[i])
k = min(k,val//diff)
for i in range(n-2,-1,-1):
diff = abs(i+1-n)
val = min(array[-1],array[i])
k = min(k,val//diff)
if k == float('inf'):
print(0)
else:
print(k)
main()
``` | output | 1 | 97,891 | 12 | 195,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,892 | 12 | 195,784 |
Tags: implementation, math
Correct Solution:
```
n=int(input())
print(min(x//max(i,n-i-1)for i,x in enumerate(map(int,input().split()))))
``` | output | 1 | 97,892 | 12 | 195,785 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,893 | 12 | 195,786 |
Tags: implementation, math
Correct Solution:
```
def relax(ans, val_, dist_):
if dist_ != 0:
return min(ans, val_ // dist_)
else: return ans
def solution(nums):
uniq = list(set(nums))
uniq.sort()
poss = dict()
for pos, num in enumerate(nums):
if num not in poss:
poss[num] = []
poss[num].append(pos)
for key in poss:
poss[key].sort()
ans = (1 << 30)
ans = relax(ans, uniq[-1], abs(poss[uniq[-1]][0] - poss[uniq[-1]][-1]))
max_pos = poss[uniq[-1]][-1]
min_pos = poss[uniq[-1]][0]
for i in range(2, len(uniq)+1):
key = uniq[-i]
max_dist = abs(poss[key][0] - poss[key][-1])
for j in [-1, 0]:
tmp = max(abs(poss[key][j] - max_pos), abs(poss[key][j] - min_pos))
max_dist = max(max_dist, tmp)
max_pos = max(max_pos, poss[key][-1])
min_pos = min(min_pos, poss[key][0])
ans = relax(ans, key, max_dist)
return ans
if __name__ == '__main__':
n = int(input())
a = list(map(int, input().split()))
print(solution(a), end="")
``` | output | 1 | 97,893 | 12 | 195,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,894 | 12 | 195,788 |
Tags: implementation, math
Correct Solution:
```
n = int(input())
sp = list(map(int, input().split()))
k = 10 ** 9 + 1
zn1 = sp[0]
zn2 = sp[-1]
ch2 = min(sp[0], zn2) // (n - 1)
if ch2 < k:
k = ch2
for i in range(1, n - 1):
ch1 = min(sp[i], zn1) // i
ch2 = min(sp[i], zn2) // (n - i - 1)
if ch2 < k:
k = ch2
if ch1 < k:
k = ch1
ch1 = min(sp[-1], zn1) // (n - 1)
if ch1 < k:
k = ch1
print(k)
``` | output | 1 | 97,894 | 12 | 195,789 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,895 | 12 | 195,790 |
Tags: implementation, math
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
ans=max(l)
for i in range(n):
k=l[i]//(max(i,n-i-1))
if(k<ans):
ans=k
print(ans)
``` | output | 1 | 97,895 | 12 | 195,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,896 | 12 | 195,792 |
Tags: implementation, math
Correct Solution:
```
n=int(input())
a=[int(x) for x in input().split()]
ans=10**12
for i in range(n):
ans=min(ans,a[i]//max(i,n-1-i))
print(ans)
``` | output | 1 | 97,896 | 12 | 195,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. | instruction | 0 | 97,897 | 12 | 195,794 |
Tags: implementation, math
Correct Solution:
```
# CODE BEGINS HERE.................
import math
n = int(input())
a = list(map(int, input().split()))
k = math.inf
for i in range(n):
# print(k)
if k > a[i]//max(n - i - 1, i):
k = a[i]//max(n - i - 1, i)
print(k)
#CODE ENDS HERE....................
``` | output | 1 | 97,897 | 12 | 195,795 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
a=[]
for i in range(n):
a.append(arr[i]//max(n-i-1,i))
print(min(a))
``` | instruction | 0 | 97,898 | 12 | 195,796 |
Yes | output | 1 | 97,898 | 12 | 195,797 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's call an array of non-negative integers a_1, a_2, β¦, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 β€ i, j β€ n the inequality k β
|i - j| β€ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a_1, a_2, β¦, a_n. Find its expansion coefficient.
Input
The first line contains one positive integer n β the number of elements in the array a (2 β€ n β€ 300 000). The next line contains n non-negative integers a_1, a_2, β¦, a_n, separated by spaces (0 β€ a_i β€ 10^9).
Output
Print one non-negative integer β expansion coefficient of the array a_1, a_2, β¦, a_n.
Examples
Input
4
6 4 5 5
Output
1
Input
3
0 1 2
Output
0
Input
4
821 500 479 717
Output
239
Note
In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| β€ min(a_i, a_j), because all elements of the array satisfy a_i β₯ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 β
|1 - 4| β€ min(a_1, a_4) = 5 is false.
In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension.
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
l1=[]
for i in range(n-1):
if n-i-1>i:
l1.append(min(l[i],l[n-1])/(n-i-1))
else:
l1.append(min(l[i],l[0])/i)
print(int(min(l1)))
``` | instruction | 0 | 97,899 | 12 | 195,798 |
Yes | output | 1 | 97,899 | 12 | 195,799 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.