We need to solve the problem: Given n integers sorted non-decreasing (1 ≤ x_i ≤ 2n) and each can be either kept or increased by 1, maximize the number of distinct values in the final sequence.
We can apply a greedy approach: process numbers from smallest to largest, and assign them to a target value if possible, ensuring uniqueness. Since each number can be incremented or not, each original number can become either x_i or x_i+1. The goal is to maximize number of distinct final values.
This is reminiscent of a classic problem: given multiset of numbers, each can be increased by at most 1, maximize distinct numbers. Usually we sort and greedy: For each number, if we haven't used the number itself, take it; else if we haven't used number+1, take number+1; else skip. But careful: In this problem, we can only increase by exactly 1 or 0, not any amount. But also we can decide to not change or increase by 1. And we want distinct numbers.
But the classic problem: "Given an array of integers, you can perform operations: increase any number by 1 any number of times, but each number only once? Actually a known problem: Minimum number of moves to make array distinct by incrementing duplicates? But here each can be increased at most once. Maximizing distinct numbers after optional increment by 1. Usually, the greedy works: sort, then maintain a set of used numbers. For each a in sorted order: if a not used, use a; else if a+1 not used, use a+1; else skip. That yields maximum distinct count. But is it always optimal? Yes, because it's like matching each original to a target, and the set of possible targets for each is {a, a+1}. Since targets are integers and we want to maximize the number of distinct targets, we can treat it as a bipartite matching between elements and possible target values, where each element can be assigned to one of two consecutive values. Sorting and greedy works because it's essentially a variation of the problem "maximum matching in a convex graph"? Another way: Consider numbers sorted. For each a, we want to assign the smallest possible target not yet used that is >= a and <= a+1. That's the typical algorithm: maintain current "available" smallest target? But we can think: We can always assign to a or a+1, but if a is already taken, the best is to assign to a+1 if not taken; if both taken, we can't assign this element to a new distinct value. However, is there any scenario where we should skip using a+1 now to leave it for later? Since numbers are sorted non-decreasing, if we process in non-decreasing order, and we prefer using the smaller target first (a), it's optimal. Because if later we have a same number a, it can't use a if we used it already, but could use a+1; if we used a+1 earlier, maybe we deprive later numbers that could have used a+1 but have no alternative? But later numbers are >= a, so they can also use a or a+1 (or higher if they are larger?). Wait each element only has options {x_i, x_i+1}. For later element with value b >= a, its options are b or b+1. Since b >= a, b might be > a+1 in some cases, so it's not necessarily that they conflict only with a+1. However, if we process in increasing order, we assign the smallest possible target for each element. This is a known optimal greedy: sort, then for each a, if a-1 hasn't been used, use it? Actually typical problem: given array, you can increase elements arbitrarily, the goal to maximize number of distinct elements by increments (each increment by 1 costs). But there's known approach: sort, then start from smallest, for each a, if a <= current target? Hmm.
Better: Let's reason carefully.
We have sorted list x_1 <= x_2 <= ... <= x_n. For each i, we choose y_i ∈ {x_i, x_i+1}. Want |{y_i}| as large as possible.
We can view as: we want to select a set of distinct integers as targets, each assigned to at least one element, with the condition that for each element i, there exists a target t_i ∈ {x_i, x_i+1} such that t_i are all distinct. The maximum number of distinct targets equals the maximum cardinality of a matching between elements and possible targets. Because we can assign each element to a distinct target if possible.
Since each element has only two possible targets, this is a bipartite graph with left side elements, right side possible integer values (the union of {x_i, x_i+1}). It's a kind of interval graph. Greedy matching in such a graph: sort elements, and for each, assign the smallest available target >= x_i (or specifically from its two options). But we must pick among its two options.
Actually, we can think of the following algorithm: iterate through sorted x_i. Keep a variable cur = 0? Or maintain a set of used numbers. For each x, we try first x: if not used, assign to x. Else if x+1 not used, assign to x+1. This is straightforward.
Proof of optimality: Since elements are sorted, for any element i, if we can assign it to x_i (the smaller option) without losing ability to assign later elements, it's safe. If x_i is already used, then we consider x_i+1. But we might also consider not assigning x_i+1 now and leaving it for later? However, later elements have values >= x_i. Suppose we have a later element j with x_j = x_i (equal) or > x_i. If x_j = x_i, then it also has options x_i and x_i+1. If we assign x_i+1 to current i, then later identical elements can only use x_i (if available) or maybe x_i+2? But they can't use x_i+2 because only +1 allowed. So they would be forced to try x_i, but x_i might be used? Actually x_i may already be used by previous element. In the scenario where x_i is already used, then for current i we consider x_i+1. But if we skip assigning i (i.e., we don't assign it to any new distinct target), could that yield a larger total? Not likely because we want to maximize count; each element can at most contribute 1 to distinct count if assigned to a new value, otherwise it duplicates something. So we always want to assign as many elements as possible to new distinct values. The greedy algorithm always assigns if possible (either to x or x+1). So we need to see if there is a case where assigning current i to x_i+1 would prevent a later element j from being assigned to a distinct value that would have given a larger overall distinct count. But note that j's options are x_j and x_j+1. If x_j > x_i+1, then x_i+1 is irrelevant to j. If x_j = x_i+1, then j's options are x_i+1 and x_i+2. In that case, if we assign current i to x_i+1, then j could still use x_i+2 if available. But could j also use x_i+1 if we didn't assign it? Possibly. But maybe j cannot use x_i+2 because it's not allowed? j's allowed: x_j+1 = (x_i+1)+1 = x_i+2, which is allowed. So j would have two options: x_i+1 (which we might have taken) and x_i+2. If x_i+2 is available, then j can use that. But if x_i+2 is also used? Possibly by other elements. However, note that j is later; there might be constraints. Could there be a scenario where we have three elements with values: a, a, a+1? Let's test: x = [a, a, a+1]. Greedy: first a -> use a. second a: a used, check a+1 -> not used, use a+1. third a+1: now a+1 used, so check (a+1)+1 = a+2, but a+2 not allowed? Wait element value a+1: options: a+1 and a+2. a+1 already used, a+2 not used, so assign to a+2. Distinct count = 3. That's maximum? Possibly yes. If we had assigned second a to something else? Actually only two options: a or a+1. So with greedy we get 3 distinct.
Consider x = [a, a+1, a+1]. Greedy: first a -> use a. second a+1: a+1 not used -> use a+1. third a+1: a+1 used, check a+2 -> use a+2. Distinct = 3. Could we get 3? Yes.
Consider x = [a, a+1, a+1, a+2]. Greedy: a->a, a+1->a+1, a+1->a+2 (since a+2 allowed? For element value a+1, options a+1 and a+2. At third element, a+1 used, a+2 not used, assign to a+2. Fourth element a+2: now a+2 used, options a+2, a+3; a+3 not used -> assign a+3. Distinct = 4. Could we get 4 distinct? Possibly yes.
Now consider a potential counterexample: Suppose we have numbers: [1,2,2]. Greedy: 1->1; 2->2; second 2: 2 used, check 3 -> use 3. Distinct=3. That's max.
What about [1,1,2]? Greedy: 1->1; second 1: 1 used, check 2 -> 2 not used, assign 2; 2: now 2 used, check 3 -> assign 3. Distinct=3. Max? Possibly yes.
Now more tricky: maybe we could skip using a+1 for a duplicate to save it for a later number that only has that one option? But note later number's options are based on its own value. If a later number has value exactly a+1, its options are a+1 and a+2. So if we assign a+1 now, later number still has a+2 as backup. So it's okay. However, if a later number has value a, then its options are a and a+1. In that case, if we assign a+1 now, then later a could still use a (if a not used) or if a is used, then later a might be forced to use a+1 but it's used, so it could only use a+2? But a+2 not allowed. So could that happen? Consider: [a, a, a]. Greedy: first a->a; second a: a used, a+1 available -> assign a+1; third a: a used, a+1 used -> cannot assign. Distinct=2. Could we do better by assigning second a to something else? It only has a and a+1. If we don't assign second a (i.e., we skip assigning to a new distinct), then we could assign third a to a+1, still distinct count=2. So no improvement. But maybe if we assign first a to a+1 instead, then second a to a, third a to ? a? But a used? Actually if first a uses a+1, then second a could use a, third a could use? a used, a+1 used, no option. Still 2. So max is 2.
Consider [a, a, a+1]. Greedy as earlier gives 3. Could there be a scenario where greedy yields less than optimal? Let's attempt to systematically prove optimality.
Alternatively, we can solve using DP? But constraints and sum n up to 1e5, O(n) per test is fine.
I recall a Codeforces problem: "Maximum distinct numbers after increment by 1". Actually it's from Codeforces Round #... I've seen a solution: sort, then for each a, if a-1 > last? Not exactly.
Another viewpoint: After transformation, each resulting value must be either the original or original+1. So we can think of constructing a set of distinct numbers: we can pick for each element one of two values. Equivalent to assigning each element to a distinct integer, and the assigned integer must be either x_i or x_i+1. This is a matching problem in a bipartite graph where the right side is all integers (maybe up to 2n+1). Since x_i <= 2n, x_i+1 <= 2n+1. But the greedy works.
We need to be careful about the condition that the input is sorted non-decreasing. That's given, but we could also sort if needed. However, the input is already sorted, which hints that we can process in order.
Thus, solution: For each test case:
- Read n.
- Read list a (sorted).
- Maintain a set (maybe boolean array or Python set) of used numbers.
- For each x in a:
- if x not in used: used.add(x)
- else if x+1 not in used: used.add(x+1)
- else: skip (do nothing).
- Answer = size of used.
But is this correct? Let's test with provided examples.
Example 1: [1,2,2,2,5,6]
Process:
1: not used -> used={1}
2: not used -> used={1,2}
2: used contains 2, check 3 -> not used -> used={1,2,3}
2: used contains 2, used contains 3? Now 2 is used, 3 is used; can't assign -> skip.
5: not used -> used add 5 -> {1,2,3,5}
6: not used -> used add 6 -> {1,2,3,5,6} -> size 5. matches output.
Example 2: [4,4]
4: not used -> {4}
4: used contains 4, check 5 -> not used -> {4,5} -> size 2.
Example 3: [1,1,3,4,4,5]
1: -> {1}
1: 1 used, check 2 -> not used -> {1,2}
3: -> {1,2,3}
4: -> {1,2,3,4}
4: 4 used, check 5 -> not used -> {1,2,3,4,5}
5: 5 used, check 6 -> not used -> {1,2,3,4,5,6} size 6. matches.
Example 4: [1] -> {1} size 1.
Example 5: [1,1,1,2,2,2]
Process:
1 -> {1}
1: 1 used, check 2 -> not used -> {1,2}
1: 1 used, 2 used -> skip
2: 2 used, check 3 -> not used -> {1,2,3}
2: 2 used, 3 used -> skip
2: skip
Size=3. Output 3. matches.
So greedy works for examples.
Now we must confirm for edge cases. For large values up to 2n, but we can add 1, so max target is up to 2n+1. But used set can be a Python set. Since total n sum 1e5, it's fine.
But is there any nuance? Input sorted non-decreasing, but not strictly. The greedy works regardless of sorting. So we can use it directly.
However, we should think about correctness in all cases. This is essentially a maximum matching in a bipartite graph where each left node (element) connects to two consecutive integers. The greedy algorithm that processes elements in non-decreasing order and always picks the smallest possible available target (i.e., first try x, then x+1) yields a maximum matching. This is due to the fact that the graph is a "laminar" structure: the intervals (options) are like points. Actually we can transform to an equivalent problem: we want to maximize distinct numbers, which is like we want to match as many elements as possible to distinct integers, with constraints. There's a known lemma: for such constraints, greedy is optimal.
Proof sketch: Consider any optimal assignment. We can transform it to the greedy assignment without decreasing the number of assigned distinct values. We process elements in order. If the greedy picks a certain target for an element and optimal picks a different one, we can swap assignments to match greedy.
Alternatively, we can view it as: we are building a set S of used numbers. The condition that we can assign each element to a distinct number in S is that for each element, there exists t in S ∩ {x_i, x_i+1} such that each t used at most once. But we are constructing S incrementally. Greedy picks the smallest available number that the element can take, which is a standard algorithm for maximum matching in a bipartite graph where right nodes are integers and left nodes have intervals of length 2. Since intervals are of length 2 and are sorted by left endpoint (x_i is non-decreasing), the greedy algorithm that matches each left node to the smallest available right node that lies in its interval yields a maximum matching (this is a special case of the Hall's marriage theorem for convex bipartite graphs). Indeed, it's known that for bipartite graphs where the neighbors of left vertices are intervals of consecutive integers, and left vertices are sorted by left endpoint, the greedy matching that processes left vertices in order and assigns the smallest available neighbor is optimal. This is used in many problems like "assign tasks to workers" etc.
Thus, solution is clear.
But we need to ensure we don't exceed time: t up to 10000, sum n 1e5. So O(n) per test with set operations is fine.
Implementation details: Input reading, for each test case, we read n, then list a. Since a is sorted, we can proceed.
Potential pitfall: Using a Python set, but we need to ensure we don't have collisions with numbers from previous test cases? We'll create new set per test.
Memory: set size up to n maybe, but n <= 1e5, fine.
Edge Cases: n=0? Not allowed as n>=1.
Now about constraints: x_i can be up to 2n, but x_i+1 can be up to 2n+1. So we need to consider numbers up to 2n+1. But since sum n <= 1e5, we don't need to preallocate large arrays, but we could use a boolean array of size 2n+2 for speed. However, using set is simpler.
But in Python, using set might be okay for 1e5 operations. But t can be up to 10^4 but sum n 1e5, so total operations across tests ~1e5 set adds and lookups. That's trivial.
Thus, solution: For each test case:
- read n
- read list a
- used = set()
- for x in a:
if x not in used:
used.add(x)
elif x+1 not in used:
used.add(x+1)
- print(len(used))
Now verify with some random tests via brute force for small n to ensure correctness.
Let's do mental test: [2,2,3]. Greedy: 2->2; 2: 2 used, 3 not used -> add 3; 3: 3 used, 4 not used? Actually options for 3: 3 and 4, but 3 used, so add 4. Distinct=3. Could we achieve 3 distinct? Yes: assign first 2->2, second 2->3, third 3->4. Good.
[2,3,3]. Greedy: 2->2; 3->3; 3: 3 used, 4->4; distinct=3. Good.
[1,2,3]. Greedy: 1->1; 2->2; 3->3 => distinct=3.
[1,1,2,2]. Greedy: 1->1; 1->2; 2->? 2 used, 3 ->3; 2->? 2 used, 3 used, so skip? Actually let's list: after processing first three: used {1,2,3}. Next element is 2: options 2,3. Both used, skip. So distinct=3. Could we get 4? Possibly assign: first 1->1, second 1->? Could assign to something else? Only 1 or 2. If we assign second 1->2, as above. For the two 2's: we could assign one to 3 and one to 4? But 2's options are 2 and 3. So maximum distinct is 3? Let's try alternative: first 1->1, second 1->? If we don't assign second 1 to a new distinct (i.e., keep as 1 duplicate), then we could assign first 2->2, second 2->3. Then distinct set would be {1,2,3} size 3. Could we get 4 by using 4? Only if some element can reach 4. A 2 can become 3 at most. 1 can become 2. So max distinct is 3. So greedy correct.
Now [1,1,1,2]. Greedy: 1->1; second 1: 1 used, 2 not used -> add 2; third 1: 1 used, 2 used -> skip; 2: 2 used, 3 not used -> add 3. Distinct=3. Could we get 4? No.
Now a more complex scenario: [1,2,2,3]. Greedy: 1->1; 2->2; 2: 2 used, 3 not used -> add 3; 3: 3 used, 4 not used -> add 4 => distinct=4. Maximum? Possibly 4. Could there be 5? No, only 4 elements.
Now test where greedy might misassign if we didn't consider using smaller number later? But because we process in non-decreasing order, smaller numbers first get their smallest possible. Could there be a case where it's better to assign a smaller number to a larger value to free its small value for a later number that can only take that small value? But later numbers have options that are at least as large as their own value, which is >= current value. If we assign current element to a larger value, we might free the smaller value for a later element that could also take the larger value? But later element might have a value equal to the smaller value, meaning it could take either the small value or its own +1. However, if we keep the small value free, then the later element could take it, but if we assign the small value now, the later element could still take its +1. So both are possible. But could there be a scenario where the later element cannot take its +1 because that +1 is already taken by another later element that has no alternative? Let's try to construct.
We need to see if greedy could lead to a situation where an element is unable to be assigned because both its options are taken, whereas if we had assigned an earlier element differently, we could have assigned both.
Suppose we have three elements: x1 = a, x2 = a, x3 = a+1. Greedy: assign a, then a+1 to second, then a+2 to third? Wait greedy for x3: options a+1 (used) and a+2 (not used) so assign a+2. All three assigned distinct: {a, a+1, a+2}. Good.
Now suppose we have: x1 = a, x2 = a+1, x3 = a+1. Greedy: x1->a, x2->a+1, x3: a+1 used, a+2 free -> assign a+2. Distinct {a, a+1, a+2}. Good.
Now suppose: x1 = a, x2 = a, x3 = a. Greedy gives two distinct: assign a and a+1, third can't. Could there be a way to get three distinct? No because max options per element: a, a+1. Only two distinct values possible.
Now consider: x1 = a, x2 = a, x3 = a, x4 = a+1. Greedy: a->a; second a->a+1; third a: both a and a+1 used -> skip; x4=a+1: options a+1 (used) and a+2 (free) -> assign a+2. Distinct {a, a+1, a+2} => 3. Could we get 4? No. Could we assign differently to get also third a assigned? If we assign first a->a, second a->? Could we assign first a->a+1 instead? Then second a->a, third a->? a and a+1 used? Actually after: first a->a+1, second a->a, third a: both a and a+1 used, skip. Then x4 = a+1: both a+1 used? Actually a+1 used, a+2 free -> assign a+2. So still 3 distinct. Could we assign first a->a+1, second a->a+2? But a+2 is not an option for a (only a or a+1). So no.
Thus greedy seems optimal.
But we must also consider that values can be up to 2n and sorted input is given. The algorithm is straightforward.
Now think about a potential edge: when we use set, we need to be careful about integer overflow? Not in Python.
One more nuance: Since x_i are sorted non-decreasing, but we have ability to increase by 1; we might want to "skip" using a value if we know that a later element might need it more because it has only one option. However, each element always has two options (except maybe if x_i+1 equals some bound? but that's fine). So the only case where an element has only one effective option is if its two options coincide? No, they are distinct because x_i and x_i+1 are different. So always two options. So each element can always choose between two numbers. The only risk is that if we assign a particular number to an earlier element, we might block a later element that can only take that number (i.e., both of its options become blocked). But if a later element has value v, its options are v and v+1. So if we block v, it can still use v+1 unless v+1 is also blocked. So the only risk is if both v and v+1 are blocked by earlier assignments. Could we have avoided that by leaving v unblocked and instead assigning earlier element to v+1 (if possible) to keep v free for later? That would require that earlier element had the option to take v+1 (i.e., either x_i = v+1 or x_i+1 = v+1). Since earlier element's value <= v (because sorted), the only way earlier element could take v+1 is if its x_i = v+1 (but then its value is larger than v, contradict sorted order if earlier index). Or if its x_i = v and we assign it to v+1, that's possible: for element with value v, it can take v+1. So consider: we have two elements: first element with value v, second element with value v. Greedy assigns first to v, second to v+1. That's fine. If we instead assign first to v+1 and second to v, we also get two distinct. Both are fine. So no issue.
Now consider: first element value v, second element value v, third element value v+1. Greedy: first->v, second->v+1, third: v+1 used, v+2 free -> assign v+2. Distinct {v, v+1, v+2}. If we assign first->v+1, second->v, third->v+1? Actually third would see v+1 used, v+2 free -> assign v+2. Same distinct. So no issue.
Now consider: first element value v, second element value v+1, third element value v+1. Greedy: v->v, v+1->v+1, third v+1: v+1 used, v+2 free -> v+2. Distinct three. If we assign v->v+1, then second v+1: options v+1 used? Actually if we assign v->v+1, then second v+1: v+1 used? v+1 is used, so it would go to v+2. Then third v+1: v+1 used, v+2 used? Now v+2 is used by second, so third would have no option. So that would give only 2 distinct. So greedy's choice (assign v to v) yields better. So greedy's preference for the smaller available option when both are free is indeed better because it reserves larger options for later elements which may have fewer alternatives.
Thus the greedy is optimal.
Implementation: We'll use set. However, we need to be careful about the fact that the set might contain numbers up to 2n+1. Since total sum n is 1e5, we can also use a boolean list of size maybe 2max_n+2 per test, but with many test cases we would allocate many arrays. Since sum n is limited, we can allocate an array of size 2n+2 for each test. That may be simpler and faster in Python? Set operations are O(1) average but with overhead. With n total 1e5, it's fine.
But t could be up to 10000, meaning many small test cases. Using a set per test might cause overhead of set creation for each test. But total operations are small. However, an alternative is to use a list/array of size up to maybe 210^5? But we need to allocate per test based on its n. Since sum n <= 1e5, we could allocate a global boolean array of size maybe 210^5+5 and reuse, resetting only the indices we used. But easier to use set.
Given the constraints, set is fine.
We should note that the input might be large, so use sys.stdin.read for fast reading.
Now, check edge cases: n=1, x=1 -> output 1.
Another case: all numbers same, say n=5, x=3 each. Greedy: first->3, second->4, third: 3 and 4 used -> skip, fourth: skip, fifth: skip -> distinct=2. Is that maximal? Could we get 3? Let's see: we have five elements, each can be 3 or 4. So at most 2 distinct numbers can appear (3 and 4). So max distinct = 2. So greedy obtains 2.
Now, what about numbers that are like 1,2,3,..., n? All distinct initially, so we should get n. Greedy: each x not used -> add, so all added, distinct=n.
Now numbers that are like 1,1,2,2,3,3,...? Could we get 2n distinct? Possibly by incrementing the duplicates to the next integer? Let's test n=2k: sequence: 1,1,2,2,...,k,k. Each pair: first uses original, second uses original+1, except last maybe uses k+1? Example: k=3: [1,1,2,2,3,3]. Greedy: 1->1; 1->2; 2->? 2 used, 3 not used -> 3; 2->? 2 used, 3 used -> skip? Wait processing in order: after first two, used {1,2}. Next element 2: 2 used, check 3 -> not used, add 3. used {1,2,3}. Next element 2: 2 used, 3 used -> skip. Next element 3: 3 used, check 4 -> add 4. used {1,2,3,4}. Next element 3: 3 used, 4 used -> skip. So distinct = 4. Could we get 5? Possibly by assigning second 2 to 3? Already did. Actually maybe better assignment: 1->1, 1->2, 2->3, 2->4, 3->5, 3->6? Wait but second 2 can become 3 (since 2+1=3), but then second 3 can become 4? Actually 3+1=4. So if we could push them: first 1->1, second 1->2, first 2->3, second 2->4, first 3->5? But 3's options are 3 and 4, not 5. So 3 cannot become 5. So maximum is 4. Actually with three distinct values initially (1,2,3), we can get at most 4? Let's think: each distinct original value v can produce v and v+1. So the set of possible target values is union over v of {v, v+1}. For v=1 => {1,2}; v=2 => {2,3}; v=3 => {3,4}. So possible values are {1,2,3,4}. So max distinct is 4. So greedy obtains 4.
Now check if greedy could ever assign a value that blocks later assignments in a way that reduces total distinct count. We argued it's optimal due to known matching property.
Thus solution is to implement greedy.
But wait: The input constraint says x_i are sorted non-decreasing and also x_i <= 2n. That ensures numbers are not huge.
Now, one more nuance: In some problems, the answer might be limited by the fact that we cannot create a number that is larger than something? But here we can create up to 2n+1, and there are n elements, so theoretically we could have up to n distinct numbers. Our greedy will achieve that if possible.
Now, I think the solution is correct.
But let's re-read the problem statement: "for each of the n notes in the song, she can either leave it as it is or increase it by 1." So each note can be increased exactly by 1 or not. So yes.
Thus answer: maximum distinct numbers.
Now, we might wonder about the sorted property: Could it be used to simplify further? Possibly we can avoid a set and just keep track of the last assigned value? But careful: Because values may not be contiguous, we might skip numbers. But we can maintain a boolean array of size up to 2n+2. Since sum n is 1e5, we can allocate for each test an array of size 2n+2, which is O(n) per test, but across tests the sum of n is 1e5, so total allocated size across tests might be sum of 2n_i+2 which is O(sum n) ~ 1e5, if we allocate per test and discard. But many test cases cause many small allocations. However, it's fine.
Alternatively, we can use a dictionary or set.
Given simplicity, use set.
Now, is there any catch that we need to output the maximum diversity, which is number of distinct notes, but maybe we can also choose not to change some notes? Already considered.
Edge Cases: When x_i+1 may coincide with some future x_j? But that doesn't matter; we treat used set.
Now, let's double-check with some random small n brute force to be absolutely sure.
We'll write a mental script: For n up to say 5, enumerate all assignments (2^n possibilities), compute distinct count, compare with greedy. But that's too much for mental. But we can test some tricky sequences.
Consider [1,3,3]. Greedy: 1->1; 3->3; 3: 3 used, 4 ->4. Distinct=3. Alternative: 1->2? But 1 can become 2. Then first 3->3, second 3->4. Distinct {2,3,4}=3. So okay.
Consider [2,2,3,3]. Greedy: 2->2; 2->3; 3->? 3 used, 4 ->4; 3->? 3 used, 4 used -> skip. Distinct {2,3,4}=3. Could we get 4? Possibly assign: first 2->2, second 2->? Could be 3; first 3->? Could be 4; second 3->? Could be 5? But 3+1=4, so second 3 could be 5? No, only 3 or 4. So possible distinct values from numbers: from 2 -> {2,3}; from 3 -> {3,4}. So union {2,3,4}. So max distinct is 3. So greedy ok.
Consider [1,2,2,3,3]. Greedy: 1->1; 2->2; 2->3; 3->? 3 used, 4 ->4; 3->? 3 used, 4 used -> skip. Distinct {1,2,3,4}=4. Could we get 5? Maybe by: 1->1, 2->2, 2->? maybe 3, 3->4, 3->5? No. So max is 4. Good.
Consider [1,1,2,3]. Greedy: 1->1; 1->2; 2->? 2 used, 3 ->3; 3->? 3 used, 4 ->4. Distinct {1,2,3,4}=4. Max? Yes.
Now consider [1,2,3,3,4]. Greedy: 1->1;2->2;3->3;3->4 (since 3 used, 4 not used? Actually check: after first three: used {1,2,3}. Next element 3: 3 used, 4 not used -> add 4. Next 4: 4 used, 5 not used -> add 5. Distinct {1,2,3,4,5}=5. Max? 5 elements, yes.
Now consider [1,2,2,2,3]. Greedy: 1->1; 2->2; 2->3; 2: both 2,3 used -> skip; 3: 3 used, 4 not used -> add 4. Distinct {1,2,3,4}=4. Could we get 5? Only 5 elements, possible distinct values from {1,2,3}. 1->{1,2}; 2->{2,3}; 3->{3,4}. So union {1,2,3,4}. So max is 4. So greedy yields 4.
Now [2,2,2,3,3]. Greedy: 2->2; 2->3; 2: both used -> skip; 3->? 3 used, 4 ->4; 3->? 3 used, 4 used -> skip. Distinct {2,3,4}=3. Could we get 4? Union of possible values: 2->{2,3}, 3->{3,4} => {2,3,4}. So max 3.
All good.
Now, more tricky: What about numbers that are not contiguous? E.g., [1,100] where n=2, x_1=1, x_2=100. Greedy: 1->1; 100->100. Distinct=2. Could we get 3? No, only 2 elements.
What about [1,1,100]? Greedy: 1->1; 1->2; 100->100 => distinct=3. Alternative: 1->1, 1->1? then 100->100 gives 2, so greedy best.
Now, consider [1,2,2,4]. Greedy: 1->1; 2->2; 2->3; 4->4. Distinct {1,2,3,4}=4. Could we get 4? Yes. Good.
Now, what about [1,1,2,4]? Greedy: 1->1; 1->2; 2->? 2 used, 3 ->3; 4->4. Distinct {1,2,3,4}=4. So fine.
Now, is there any case where we might want to not assign a number to a distinct value even if both options are free, in order to achieve a higher total? That seems counter-intuitive because each element can at most contribute one to distinct count, so assigning a new distinct value always increases distinct count by 1 (unless it collides with a later assignment that would have contributed same value? But if we assign now, we may block later element from being assigned, but if we skip now, we might assign later element to a new distinct value, but we lose the current contribution. So the trade-off could be: if by assigning now we block a later element that could have contributed a distinct value that otherwise would be impossible to get, we might net fewer distinct values. But does such scenario exist? That would require that later element can only be assigned to a value that we are using now, and that later element cannot use any other value, while current element could use some other value that is also free now but we chose not to use that other value. In other words, suppose current element with value v has two free options: v and v+1. If we assign it to v, we block v, but v+1 remains free. Later element w has options w and w+1. For it to be blocked only if we assign to v, we need that both its options become blocked because of our assignment, i.e., w and w+1 are both used after our assignment. But if we assign to v+1 instead, maybe w still has an option free. So the question: can there be a scenario where assigning to the smaller option (v) leads to later element being unable to assign, while assigning to the larger option (v+1) leaves a free option for later element, resulting in higher total? But our greedy picks the smaller option if available. So if both v and v+1 are free, greedy picks v. Could that be suboptimal? Let's try to construct.
We need later element L such that its only free option would be v+1 if we assign v to v, but if we assign v to v+1, then v is free and L can use v? But L's options are w and w+1. For v to be one of its options, we need w = v or w+1 = v. So either w = v or w = v-1. Since numbers are sorted non-decreasing, later element's value w >= v (because sorted). So w could be v (if there are multiple v's) or > v. If w > v, then w >= v+1. So w could be v+1. So possibilities:
-
If later element has w = v, then its options are v and v+1. If we assign current element to v, then later element still can use v+1 (if free). If we assign current to v+1, later element can use v (if free). So both assignments give one free option for later element, assuming the other option remains free. But if both v and v+1 are currently free, and we assign one, the other remains free. So later element would still have one free option regardless of which we pick. So no disadvantage.
-
If later element has w = v+1, then its options are v+1 and v+2. If we assign current to v, then v+1 remains free, so later element can use v+1. If we assign current to v+1, then v+1 becomes used, but v+2 might be free, so later element could use v+2. So again, later element still has a free option. So no problem.
-
If later element has w > v+1, then its options are w and w+1, both > v+1. So our assignment to v or v+1 does not affect its options. So no problem.
Thus, if both options are free initially, picking either is fine, because later element's options may be affected but there will always be at least one free option (unless something else blocks the other). But could picking v cause both options for later element to be blocked, while picking v+1 would leave one free? For both options to be blocked, both w and w+1 must be used. Our assignment to v only uses v. For both w and w+1 to be used, they must be used by other assignments. Could our assignment to v (instead of v+1) cause a chain that leads to w and w+1 being used? It's unlikely, because the difference is only swapping which of the two numbers is used by current element. So if picking v leads to a scenario where later w is forced to use v+1 and later another element uses v+2 etc., it might cause a blockage, but picking v+1 might also cause blockage elsewhere. We need to check if there is a known counterexample.
Let's attempt to search for a counterexample systematically for small sequences. Since we have the sorting condition, maybe the greedy algorithm is indeed optimal. I recall a Codeforces problem: "C. Increase Subarray Sums"? Not that. Another: "B. Diversity" perhaps. Actually I think there is a problem named "Diversity" where you can increase each element by at most 1 and maximize distinct. And the solution is exactly the greedy described. So it's correct.
Thus, we'll implement it.
Now, regarding time: t up to 10000, sum n up to 1e5. So we must be careful about input reading. We'll use sys.stdin.buffer.read and split.
Now, code in Python:
def solve():
import sys
data = sys.stdin.buffer.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
used = set()
for i in range(n):
x = int(next(it))
if x not in used:
used.add(x)
elif x+1 not in used:
used.add(x+1)
out.append(str(len(used)))
sys.stdout.write("\n".join(out))
But note: The input constraints say x_1 ≤ x_2 ≤ … ≤ x_n. But we are not relying on that for algorithm correctness? Actually the greedy works even if unsorted. However, we may process them in given order which is sorted. That's fine.
Now, test with given example.
Works.
Now, we must consider memory: set used may hold up to n entries. For each test case, we create new set. That's okay.
Now, is there any situation where we could get more distinct by not assigning a value to an element even if both options are free? That would be suboptimal because we could assign it and increase distinct count by 1. The only reason to skip is if assigning now would reduce the number of distinct we can get from later elements by more than 1. But as argued, each assignment uses one number that could have been used by at most one later element. Since we increase distinct count by 1 for the current assignment, and if we prevent a later element from being assigned, we lose at most 1. So net change is at worst 0. But could it be that by skipping, we could get +2 later? No, because each later element can contribute at most 1 distinct. So skipping a current element could at best allow one later element to be assigned that otherwise wouldn't, but we lose the current assignment, so net same. However, if skipping allows two later elements to be assigned (which otherwise would be blocked) then we could gain. But can a single current assignment block two later elements? No, because it occupies only one value. That one value could be the only possible free value for two different later elements? But a value can be assigned to only one element, so it can only be used by one later element anyway. So blocking one value can block at most one later element. Thus, assigning now yields +1 now, and might block at most 1 later, so net >=0. So it's never detrimental to assign if possible. However, this argument assumes that we always assign to some value if possible, and that there is always a value we can assign to that won't block a later element that could otherwise be assigned to a different value? But we have two options. So if both options are free, we can choose one that minimizes blocking. So we should always assign if at least one option is free, and we should choose the option that is less likely to cause blocking. Greedy chooses the smaller option. Is that always the less blocking? Possibly yes because smaller numbers are more likely to be needed by later elements? Since later elements have larger or equal values, they may need larger numbers. So smaller numbers are less useful for later elements? Actually later elements have values >= current, so they can use numbers >= their value. So a small number like x may be less useful for later elements because they might not be able to go down; they can only take numbers >= their own value. So if current x is small, using the small number may block a later element that also has value x (i.e., duplicates). But using x+1 might block a later element that has value x+1. Which is more critical? If there are more duplicates of x, then using x might be better? Wait, consider a case: values: [a, a, a+1]. Greedy uses a for first, a+1 for second. That gives both a and a+1 used, leaving a+2 for third. That yields 3 distinct. If we instead used a+1 for first and a for second, same. So no difference. If we have [a, a+1, a+1], greedy uses a, a+1, a+2. If we used a+1 for first, then second a+1 would use a+2, third a+1 would have no option. So that's worse. So in that scenario, using the smaller option (a) for the first element allowed us to get more distinct. So smaller option is indeed better because it reserves the larger option for potential later duplicates of the larger value. So greedy picks smaller when both free, which is optimal.
Thus algorithm is sound.
Now we might consider an alternative implementation without set, using a variable "next_available" but because values are not necessarily contiguous, it's tricky. The set approach is simplest.
One more thing: The input numbers are up to 2n, and after increment, can be up to 2n+1. Since sum n is 1e5, the numbers across tests could be up to 2*1e5, but using set is fine.
Now, let's consider potential pitfalls: In Python, set lookups and adds are O(1) average. Should be fast enough.
Now we can finalize solution.
However, we should test on random cases with a brute force to be absolutely sure. Since I'm not executing code now, I'll do mental brute for a few more.
Case: [1,1,1,2,2]. Greedy: 1->1; 1->2; 1: 1,2 used -> skip; 2->? 2 used, 3 ->3; 2->? 2 used, 3 used -> skip. Distinct {1,2,3} = 3. Could we get 4? Let's see possible values from numbers: 1->{1,2}; 2->{2,3}. So union {1,2,3}. So max distinct = 3. Good.
Case: [1,2,2,2,3,3]. Greedy: 1->1; 2->2; 2->3; 2: 2,3 used -> skip; 3->? 3 used, 4 ->4; 3->? 3 used, 4 used -> skip. Distinct {1,2,3,4}=4. Union of possible values: 1->{1,2}; 2->{2,3}; 3->{3,4} => {1,2,3,4}. So max 4. Good.
Case: [2,2,3,3,4,4]. Greedy: 2->2; 2->3; 3->? 3 used, 4 ->4; 3->? 3 used, 4 used -> skip; 4->? 4 used, 5 ->5; 4->? 4 used, 5 used -> skip. Distinct {2,3,4,5}=4. Union: 2->{2,3}; 3->{3,4}; 4->{4,5} => {2,3,4,5} => 4. So okay.
Now test with numbers that have gaps: [1,3,3,3]. Greedy: 1->1; 3->3; 3->4; 3->? 3,4 used -> skip. Distinct {1,3,4}=3. Could we get 4? Options: 1->{1,2}; 3->{3,4}. So union {1,2,3,4} but we have only 4 elements, can we achieve all 4? Possibly assign: 1->2; first 3->3; second 3->4; third 3->? 3 and 4 used, cannot get 1? But 1 is free if we didn't use it. Actually we could assign: 1->1; first 3->3; second 3->4; third 3->? maybe 2? No, 3 can't become 2. So we can't get both 1 and 2 because only element 1 can produce 2. So we have to choose either 1 or 2 from the first element. If we choose 2, then we have 2,3,4 distinct (3 numbers). If we choose 1, we have 1,3,4 (3 numbers). So max 3. So greedy gives 3.
Now test with [1,2,4,4]. Greedy: 1->1; 2->2; 4->4; 4->5. Distinct {1,2,4,5}=4. Could we get 4? Yes.
Now test with [1,2,2,4,4]. Greedy: 1->1; 2->2; 2->3; 4->4; 4->5. Distinct {1,2,3,4,5}=5. Could we get 5? Options: 1->{1,2}; 2->{2,3}; 4->{4,5} => union {1,2,3,4,5}. So yes.
Now, test with [1,1,2,2,3,3,4,4] (8 elements). Greedy will produce distinct count? Let's simulate: 1->1; 1->2; 2->? 2 used, 3 ->3; 2->? 2 used, 3 used -> skip? Wait after first three: used {1,2,3}. Next element is second 2: options 2 used, 3 used -> skip. Then 3->? 3 used, 4 ->4; next 3->? 3 used, 4 used -> skip; 4->? 4 used, 5 ->5; next 4->? 4 used, 5 used -> skip. So used {1,2,3,4,5} = 5 distinct. Could we get more? Union of possible values: 1->{1,2}; 2->{2,3}; 3->{3,4}; 4->{4,5} => {1,2,3,4,5} = 5. So max is 5. So greedy yields 5.
Now, what about [1,2,3,4,5]? Greedy yields 5. Good.
All seems consistent.
Thus, implement.
Now, we might also note that the answer cannot exceed n, but also cannot exceed the number of distinct possible values in the union of {x_i, x_i+1}. But we don't need to compute that.
One more nuance: The problem statement says x_i are sorted non-decreasing. However, they also say 1 ≤ x_1 ≤ x_2 ≤ … ≤ x_n ≤ 2·n. This constraint ensures the numbers are not too large. Not needed for algorithm but good.
Now, regarding the output: each test case output one integer.
Thus, final answer.
But we might want to write an alternative using an array for speed: Since x_i ≤ 2n, we can use a boolean array of size maybe 2n+3. But we need to allocate per test based on n. Let's consider doing that to avoid set overhead for many small test cases? But it's fine.
Now, we need to write the solution explanation. But as ChatGPT, we are to output only the code? The user asked: "Given the song as a sequence of integers describing the notes, find out the maximal, achievable diversity." The prompt is "You are given a problem statement, solve it." So we need to output code. But in this environment, we should output the final solution code. Usually, we provide a python solution.
But the instructions: "You are an AI that provides final answer. The assistant should output the code only." However, the conversation says: "Solve the problem." Possibly expecting the code. So we will output Python code.
But careful: The initial "system" says: "You are an AI designed to assist with math and coding problems. The user will provide a problem statement, and you need to output the solution code." So we need to output code.
Thus, final answer: Provide Python code with function solve() and reading input.
Let's produce the code.
But also note: Some online judges require a main guard. Not necessary.
We'll write:
import sys
def solve():
data = sys.stdin.buffer.read().split()
t = int(data[0])
idx = 1
out = []
for _ in range(t):
n = int(data[idx]); idx += 1
used = set()
for i in range(n):
x = int(data[idx]); idx += 1
if x not in used:
used.add(x)
elif x+1 not in used:
used.add(x+1)
out.append(str(len(used)))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
That should work.
We need to be careful about index bounds. Since sum n <= 1e5, data length is enough.
Now, potential improvements: We could avoid using set by using an array of size up to 2n+2, but I'll stick with set.
Now, test with given example:
Input:
5
6
1 2 2 2 5 6
2
4 4
6
1 1 3 4 4 5
1
1
6
1 1 1 2 2 2
Our code outputs:
5
2
6
1
3
Matches.
Thus, final answer.
But let's think about an edge: When we check "x not in used", it's a hash lookup. Fine.
Time complexity: O(total n) with constant factor.
Now, final output: Provide code.
However, there is a nuance: The greedy algorithm described earlier might need to consider that after incrementing a note by 1, it could become equal to some other note that is also incremented or not. But we handle that via set.
One more thing: The input might contain values up to 2n, but also we might add 1 to get 2n+1. Could there be a collision where 2n+1 is used and later another element wants to use it? But it's fine.
Now, we can also solve without set by using a boolean list of size maybe 2*n+5, but for simplicity we use set.
Now, we should consider that the number of test cases can be large, but sum n is limited, so creating a new set for each test case is okay.
Now, finalize.
But wait, there is another approach: Since the numbers are sorted and we can only increase, we can just iterate and maintain a variable "next_target" maybe. Let's explore if we can achieve O(1) space (excluding input). Possibly we can simulate with a variable "cur" representing the smallest integer that we haven't used yet that is at least something? But because we have gaps, it's not trivial. However, we might note that we only care about distinct count, and we can simulate greedy by checking if we can assign each x to a new value without tracking which specific values, just count? That seems impossible because we need to know which values are used to avoid collisions. However, we can treat it as we are building a set of used numbers, but we could also think in terms of "if x > last_assigned" etc. But due to gaps, we might assign to x even if x is not > last_assigned. I'd rather use set.
But perhaps there's a simpler linear scan with O(1) extra memory: Since we process sorted numbers, we can maintain a variable "next" but it's tricky. Let's think: We want to assign each x to the smallest available integer from {x, x+1} that hasn't been used before. Since we process in increasing order of x, the used numbers will be a set of integers. We can try to maintain a boolean array of size maybe up to max value. But given constraints, it's fine.
Thus, final answer: code as above.
Now, provide the final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
t = int(data[0])
idx = 1
out_lines = []
for _ in range(t):
n = int(data[idx]); idx += 1
used = set()
for _ in range(n):
x = int(data[idx]); idx += 1
if x not in used:
used.add(x)
elif x + 1 not in used:
used.add(x + 1)
out_lines.append(str(len(used)))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()