N Soldiers are lined up for a memory test. They are numbered from 0 to N-1 from left to right.
In the test, there are M rounds. In each round, Captain selects one position. Soldier at that position will be numbered 0. All the soldiers to the right of selected position will be numbered one greater than the soldier to his left. All the soldiers to the left of selected position will be numbered one greater than the soldier to his right.
eg. if N = 6 and selected position is 3, then the numbering will be [3, 2, 1, 0, 1, 2].
After M rounds, Captain asked each soldier to shout out the greatest number he was assigned during the M rounds. In order to check the correctness, Captain asked you to produce the correct values for each soldier (That is the correct value each soldier should shout out).
Input
The first line of the input contains an integer T denoting the number of test cases.
First line of each test case contains two integers, N and M.
Second line of each test case contains M integers, the positions selected by Captain, in that order.
Output
For each test case, output one line with N space separated integers.
Constraints
1 ≤ T ≤ 10^4
1 ≤ N ≤ 10^5
1 ≤ M ≤ 10^5
1 ≤ Sum of N over all testcases ≤ 10^5
1 ≤ Sum of M over all testcases ≤ 10^5
0 ≤ Positions selected by captain ≤ N-1
Example
Input
2
4 1
1
6 2
2 3
Output
1 0 1 2
3 2 1 1 2 3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve this problem: There are N soldiers indexed 0..N-1. For each round, captain selects a position p. Then soldiers are numbered based on that position: soldier at p gets 0; for soldiers to the right: each soldier gets number = distance from p? Actually description: "All the soldiers to the right of selected position will be numbered one greater than the soldier to his left. All the soldiers to the left of selected position will be numbered one greater than the soldier to his right." That implies: For any soldier i > p: number = number of soldier at i-1 + 1? Actually typical pattern: If you set position p to 0, then to the right you increment by 1 each step, so soldier i gets (i-p). And to the left you also increment by 1 as you go left, so soldier i gets (p-i). That yields [p-0?]. Wait: For left side: soldier at p-1 gets 1, p-2 gets 2, etc. That is (p-i). So indeed numbering for a round with position p is: a[i] = |i - p|. Yes, it's absolute difference. So each round produces numbers as absolute differences.
After M rounds, each soldier should output the maximum number he was assigned across all rounds. So for each soldier i, we need max_{round j} |i - p_j|.
Given N up to 1e5 per test case, M up to 1e5, sum of N and M across test cases <= 1e5 each? Actually constraints: Sum of N over all testcases ≤ 10^5, Sum of M over all testcases ≤ 10^5. So total across all tests is manageable.
We need to compute for each i (0..N-1) the maximum distance to any of the selected positions.
Essentially, for each i, it's the farthest distance to any p in the set of positions (list of M positions, but could have duplicates). Since we take max across rounds, duplicates don't affect max. So we can consider the set of distinct positions, but we can also just process all.
Observation: The maximum distance for a point i among a set of points is max( i - min(p), max(p) - i ) if we consider positions sorted. Because the farthest distance is either to the leftmost selected position or the rightmost selected position. Indeed, for any set of points on a line, the farthest point from i is either the leftmost or rightmost point. So we just need minP = minimum selected position index, and maxP = maximum selected position index across all rounds. Then answer for soldier i is max( i - minP, maxP - i ). But careful: if i is between minP and maxP, then the farthest distance is to whichever endpoint is farther from i: i-minP vs maxP-i. So yes.
Wait, but is it always? Suppose we have points at 2 and 5. For i=3, distances: |3-2|=1, |3-5|=2 -> max=2 = max(3-2=1,5-3=2) = 2, correct. For i=0, distances: 2,5 -> max(2,5) actually minP=2, maxP=5: i - minP is negative? Actually i-minP = 0-2 = -2, but we take absolute? The expression max( i - minP, maxP - i ) would give max(-2,5) = 5, but actual max distance is max(|0-2|=2,|0-5|=5)=5. So to compute correctly we need max( maxP - i, i - minP )? Actually i-minP could be negative if i < minP. But we want distance, which is positive. However if i < minP, then farthest is to maxP? Let's think: If i is left of minP, then the farthest point is maxP (since maxP is farther than minP). Distance to maxP = maxP - i; distance to minP = minP - i (positive). But maxP - i > minP - i. So the maximum is maxP - i. So in that case, the maximum is maxP - i. Similarly if i > maxP, then max distance is i - minP. If i is between minP and maxP, the max distance is max( i - minP, maxP - i ). So the formula: answer[i] = max( maxP - i, i - minP ). Actually need to consider absolute values? Let's derive:
Let L = minP, R = maxP. For any i, the farthest distance to any point in set S (which includes all points between L and R as well? Actually set may not contain all points, but farthest is either L or R) is max( |i - L|, |i - R| ). And |i-L| = if i>=L then i-L else L-i; similarly for R. But we can compute as:
If i <= L: then both i-L negative, but absolute gives L-i; i-R gives R-i? Actually if i <= L <= R, then distance to R is R-i, to L is L-i; R-i >= L-i, so max = R-i = maxP - i.
If i >= R: then distance to L is i-L, to R is i-R; i-L >= i-R, so max = i-L = i - minP.
If L <= i <= R: then distances: i-L and R-i. So max = max(i-L, R-i).
So answer[i] =
if i <= L: R - i
elif i >= R: i - L
else: max(i-L, R-i)
We can unify as answer[i] = max( i - L, R - i ). But careful: if i < L, then i - L is negative, while R - i is positive, so max would be R - i. Good. Similarly if i > R, R - i negative, i - L positive, max gives i - L. So indeed answer[i] = max( i - L, R - i ) works because one of the terms may be negative but the max picks the positive larger one which yields the correct distance. But is it guaranteed that max(i-L, R-i) equals the farthest distance? Let's test: i=0, L=2, R=5. i-L = -2, R-i = 5 -> max = 5, correct. i=6, L=2,R=5: i-L=4, R-i= -1 -> max=4, correct (distance to L=4, to R=1, max=4). i=3: i-L=1, R-i=2 -> max=2, correct (distance to R). So indeed answer[i] = max( i - L, R - i ). That's a neat formula.
Thus the problem reduces to: for each test case, find the minimum and maximum selected positions across M rounds. Then compute for each i (0..N-1): max(i - minP, maxP - i).
But wait, is there any nuance? The description says "After M rounds, Captain asked each soldier to shout out the greatest number he was assigned during the M rounds." That is the maximum across rounds. Indeed, each round we assign numbers = |i-p|. So the max is max over p in P of |i-p|. So as argued, that's max(|i - minP|, |i - maxP|) = max(i - minP, maxP - i) if we consider that i - minP may be negative etc. So it's correct.
Edge cases: M could be 0? Constraint M>=1. But if M=0, then no round? But not needed.
We need to output for each soldier the maximum. Complexity O(N) per test case after finding min and max (O(M)). Since sum of N across tests <= 1e5 and sum of M <= 1e5, it's fine.
But wait, is it that simple? Let's verify with given examples.
Thus solution is trivial: For each test case, read N, M, then read M positions. Track minPos, maxPos. Then output for i from 0 to N-1: max(i - minPos, maxPos - i). Possibly need to use long? N up to 1e5, distances up to 1e5, so int fine.
But wait: Could there be any scenario where the farthest point is not one of the extremes? Let's think: The maximum distance from a point to a set of points on a line is indeed the maximum of distances to the extreme points. Because for any point p between L and R, its distance to i is less than or equal to max(|i-L|,|i-R|). But could there be a scenario where a point outside L..R gives larger distance? L and R are min and max, so no point outside that interval. So yes.
Thus the solution stands.
Given the constraints sum of N <= 1e5, we can output per test case straightforwardly.
Implementation: For each test case, read N, M; then read M ints; find min, max. Then loop i=0..N-1 compute answer.
But caution: Input size: T up to 1e4, but sum of N and M is small. So reading per test case is fine.
Potential pitfalls: The positions are 0-indexed. So minPos could be 0, maxPos N-1. Then answer: i - minPos = i, maxPos - i = (N-1)-i, so answer = max(i, N-1-i). That is symmetrical as expected.
Thus final answer.
But wait, the problem statement: "All the soldiers to the right of selected position will be numbered one greater than the soldier to his left. All the soldiers to the left of selected position will be numbered one greater than the soldier to his right." This yields exactly absolute differences. So yes.
Thus the problem is very easy.
But we need to ensure output format: N space-separated integers per line.
Time complexity: O(total N + total M).
Now implement.
Edge Cases:
M could be large but sum M small.
There might be duplicates, but min/max still fine.
N=1: then minPos = maxPos = 0. For i=0: max(0-0=0,0-0=0)=0. Output: 0.
Works.
Thus solution.
But wait, is there any trick about reading input? Possibly positions are in one line, but M may be large, but sum M small, so fine.
Let's double-check constraints: "1 ≤ Sum of N over all testcases ≤ 10^5" and "1 ≤ Sum of M over all testcases ≤ 10^5". So total across test cases is small, so easy.
Now write code:
Pseudo:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it)); m = int(next(it))
minPos = 10**9; maxPos = -1
for _ in range(m):
p = int(next(it))
if p < minPos: minPos = p
if p > maxPos: maxPos = p
# compute answers
res = [str(max(i - minPos, maxPos - i)) for i in range(n)]
out_lines.append(' '.join(res))
sys.stdout.write('\n'.join(out_lines))
That's it.
But is there any scenario where minPos or maxPos is not set? M >=1, so yes.
Potential overflow? i - minPos and maxPos - i are ints, fine.
Thus answer.
But I'm suspicious: The problem seems too trivial for a coding contest? It's maybe a trick? Let's check if the interpretation is correct: "All the soldiers to the right of selected position will be numbered one greater than the soldier to his left. All the soldiers to the left of selected position will be numbered one greater than the soldier to his right." That indeed yields a sequence like: for p=3 in N=6: left side: soldier left of p gets 1? Actually: "All the soldiers to the left of selected position will be numbered one greater than the soldier to his right." So start at p, number 0. Then immediate left (p-1) gets 1 (since right neighbor is p with 0, one greater than his right). Then p-2 gets one greater than p-1, so 2, etc. So it's absolute difference. So correct.
Thus indeed max across rounds is simply farthest from extremes.
Therefore solution is simple.
But wait, we need to confirm that the rounds happen sequentially, and the soldier shouts out the greatest number he was assigned during the M rounds. That is exactly max over rounds. So our approach is correct.
Thus final answer.
I'll produce final solution explanation and code.
But before finalizing, let's test more cases: N=5, M=3 positions: 0,2,4. min=0, max=4. Then answers: i=0: max(0-0=0,4-0=4)=4; i=1: max(1-0=1,4-1=3)=3; i=2: max(2,2)=2; i=3: max(3,1)=3; i=4: max(4,0)=4. That matches max distances: to 0 or 4 whichever farthest. Actually for i=2, distances to extremes are 2 and 2, but maybe there is a position 2 that gives 0? But max across rounds: includes 0 distance at round with p=2. But max is 2 from extremes. So answer is 2. That seems correct.
What about positions only on one side? It's fine.
Thus solution is correct.
Now, I'll implement in Python.
But given sum of N up to 1e5, using list comprehension is fine.
We need to be careful about input reading for large T but small total numbers.
Edge: There might be trailing spaces etc.
All good.
Thus final answer.
But let's consider if any nuance: The problem statement might be from CodeChef "MCHAIRS" or something? Actually maybe it's from some contest. Anyway, solution is straightforward.
Thus final. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
code
for _ in range(t):
n = int(next(it))
m = int(next(it))
# find the smallest and the largest selected position
min_pos = 10**9
max_pos = -1
for _ in range(m):
p = int(next(it))
if p < min_pos:
min_pos = p
if p > max_pos:
max_pos = p
# compute answers for all soldiers
res = [str(max(i - min_pos, max_pos - i)) for i in range(n)]
out_lines.append(' '.join(res))
sys.stdout.write('\n'.join(out_lines))