We need to compute ranks based on the described system: For each student, the rank is 1 + (number of students who have marks strictly greater than his/her marks). But if there are ties, they get the same rank, which is exactly that formula because if two students have equal marks, they both have the same count of students above them (i.e., those with higher marks). However, the rank for a student with marks x is computed as: rank = 1 + (# of students with marks > x). That gives the same rank for all students with marks x. So it's essentially a standard competition ranking ("1224" ranking).
But careful: The example: Marks: 98,68,74,21,74.
- For 98: # > 98 = 0 -> rank 1.
- For 74: # > 74 = 1 (only 98) -> rank 2. Both get rank 2.
- For 68: # > 68 = 3 (98,74,74) -> rank 4.
- For 21: # > 21 = 4 -> rank 5.
Matches output: 1 4 2 5 2. Note student 2 got rank 4, student 4 got rank 5.
Thus the ranking is simply: For each marks, rank = 1 + number of distinct marks that are greater? Actually it's count of students with higher marks, not distinct marks. Since if there are multiple students with the same higher mark, they all count. So yes, it's based on the counts.
So we can compute for each student: sort the marks descending, assign ranks based on the count of higher marks. But we need to output ranks in the original order. So typical approach: create a copy of marks, sort descending, then create a map from marks to rank: The rank for a mark is the index of its first occurrence in the sorted list + 1? Actually careful: In sorted descending order, the first element (highest) gets rank 1. Then when we encounter next distinct mark, its rank is (previous count of elements before it) + 1 = the position (1-indexed) of its first occurrence. However, if there are ties, they share the same rank. So if we sort descending and iterate, we can assign rank = i+1 to the first occurrence of a new mark, and for subsequent same marks, the same rank. But note that i is index (0-indexed). So for first element (i=0): rank=1. For next element if same marks, also rank=1? That's not correct: if same marks as previous, they should share rank. Actually in sorted descending, if first two are both 98, then both should have rank 1, because there are 0 students with higher marks. So for first occurrence of a mark, rank = 1 + count of previously processed students (i). But i counts the number of students processed so far. For the first student, i=0 => rank=1. If the next student also has the same marks, then i=1, but we must not change rank; they should also have rank=1. However, if we assign rank based on i, we would assign rank=2 for the second 98. That's wrong. So we need to assign rank based on number of distinct higher marks or based on the count of students with strictly higher marks. One approach: After sorting descending, we can compute for each student the number of students with strictly higher marks = count of marks greater than this mark. This can be done by iterating sorted list and keeping track of counts. For each distinct mark, we can precompute the count of students with higher marks. For example: sorted = [98, 98, 74, 74, 68, 21]. Then for mark 98, count of higher = 0. For mark 74, count of higher = number of 98's = 2. For mark 68, count of higher = 2+2 = 4? Actually number of students with marks > 68 = number of 98 and 74 = 2+2=4. For 21, count = 2+2+1? Actually there are 298 + 274 + 1*68 = 5? Wait, total students is 5, but in this example there is only one 68. Actually original has only one 98, two 74, one 68, one 21. So sorted = [98,74,74,68,21] (descending). Then for 98, higher=0; for 74, higher=1; for 68, higher=3; for 21, higher=4. So that works. So if we create a map from mark to rank = 1 + count of students with higher marks.
Implementation: For each test case:
- Read n.
- Read array A of length n.
- Create a copy B = sorted(A, reverse=True).
- Compute prefix counts: As we iterate B, we need to know for each value how many strictly greater elements have been encountered. Since the array is sorted descending, the number of elements strictly greater than the current element is the number of elements seen so far that are > current. But if there are duplicates, we need to treat them as a group. So we can iterate through B, maintaining a variable greater_count = 0, and a map mark_rank. For each distinct value, we set rank = greater_count + 1, then add the frequency of this value to greater_count. But careful: The map will store the rank for that mark. However, when we later assign rank to each student in original order, we just look up that mark's rank.
But caution: There might be duplicate marks across groups? Actually distinct marks are groups. So we can process by scanning the sorted array, but if we process each element individually, we need to compute its rank on the fly. For a given element at index i, the number of strictly greater marks is the count of elements before it that are > current. Since sorted descending, all elements before it are >= current. But we need strictly greater. So we can't just use i; we need to know how many are strictly greater. We can maintain a count of previous elements that are strictly greater than the current element, which essentially equals the total count of elements before i that are not equal to current. This can be computed by scanning and counting frequencies. A simpler method: create a frequency map of marks, then sort distinct marks in descending order, then compute cumulative counts. Then for each distinct mark, rank = cumulative_previous + 1. Then assign each student's rank from that map.
Steps:
- For each test case:
a. Read n.
b. Read list A of n integers.
c. Create a dictionary freq: count marks.
d. Get distinct marks sorted in descending order.
e. Initialize cumulative = 0.
f. For each mark in sorted_desc:
rank = cumulative + 1
store rank in a dict rank_of[mark] = rank
cumulative += freq[mark]
g. For each original mark in A, output rank_of[mark] with space separation.
h. Print newline.
Check example:
freq: {98:1, 74:2, 68:1, 21:1}
Sorted distinct descending: [98,74,68,21]
For 98: rank = 0+1=1, cumulative=1
74: rank = 1+1=2, cumulative=3
68: rank = 3+1=4, cumulative=4
21: rank = 4+1=5, cumulative=5
Output: [1,4,2,5,2] correct.
Edge cases: all marks equal. Then distinct list has one element. rank = 1, cumulative= n. All students get rank 1.
Constraints: n up to 10^4, T up to 50, A[i] up to 1e9. This approach is O(n log n) per test case, fine.
Make sure output formatting: one line per test case, space-separated, no trailing space? Usually okay to have a space after each except last. We can handle by joining with space.
Now the problem might expect direct scanning without using map? But that's fine.
Check sample: Input format: first line T, then for each test case, a line with n, then a line with array. Multiple test cases? The sample shows T=1, but the description says "Input: The first line consists of the number of test cases(T)." So we need to loop.
Potential pitfalls: The array might be large, reading input efficiently. Use sys.stdin.read to read all. Or use input() but n up to 10^4 so fine.
Now verify with another scenario: n=5, marks: 100,100,100,100,100 -> all rank 1. Our method: distinct marks [100], rank=1. OK.
Marks: 90,80,70,60 -> distinct sorted desc: [90,80,70,60]; ranks: 1,2,3,4. Output: 1 2 3 4. But by formula: For 90, higher=0 ->1; 80: higher=1 ->2; 70: higher=2 ->3; 60: higher=3 ->4. OK.
Marks: 100,90,90,80 -> sorted distinct: [100,90,80]; freq: 100:1, 90:2, 80:1. Ranks: 100->1, 90->2, 80->4? Wait: 90: cumulative previous =1 => rank 2; cumulative becomes 3 after adding 2. Then 80: rank=3+1=4. So output: for marks [100,90,90,80] -> [1,2,2,4]. Check manually: 100 highest ->1. For 90, higher=1 ->2, both get 2. For 80, higher=3 (100 and two 90s) ->4. OK.
Thus algorithm correct.
Now produce the final answer: Provide solution explanation and code.
But note: The problem statement says: "If two students of the class get equal marks, they are given the same rank but students with different marks are given the rank as the (number of students above him + 1)." This matches our formula.
Now about output: "One line consisting array corresponding to rank of each student of the class." So print in order.
We'll implement in Python.
Potential memory: freq dictionary with up to n entries, but n <= 10^4, fine.
Edge Cases: n=1 -> rank=1.
Now writing final solution.
We'll provide both description and code.
Will be careful about reading input:
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
T = int(next(it))
out_lines = []
for _ in range(T):
n = int(next(it))
marks = [int(next(it)) for __ in range(n)]
freq = {}
for m in marks:
freq[m] = freq.get(m,0)+1
distinct_sorted = sorted(freq.keys(), reverse=True)
rank_of = {}
cum = 0
for m in distinct_sorted:
rank_of[m] = cum + 1
cum += freq[m]
out_lines.append(' '.join(str(rank_of[m]) for m in marks))
print('\n'.join(out_lines))
That should work.
But double-check: The problem sample shows input and output exactly. Also might need to handle multiple test cases and print each output on separate line.
Complexity: O(n log n) per test case due to sorting distinct keys, but distinct <= n, so fine.
Alternative: Could also sort the array with indices and assign ranks while iterating, but the map approach is simpler.
Now provide final answer.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
marks = [int(next(it)) for _ in range(n)]
# frequency of each mark
freq = {}
for m in marks:
freq[m] = freq.get(m, 0) + 1
# distinct marks in descending order
distinct_desc = sorted(freq.keys(), reverse=True)
# assign rank to each distinct mark
rank_of = {}
cnt_higher = 0 # number of students with strictly higher marks
for mark in distinct_desc:
rank_of[mark] = cnt_higher + 1
cnt_higher += freq[mark]
# produce output in original order
out_lines.append(' '.join(str(rank_of[m]) for m in marks))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
solve()