We need to sort a list of (country-code, runs) pairs such that:
- Overall, sorted by country code in ascending order.
- For entries with the same country code, sort by runs in descending order.
Constraints: N up to 1e6. Runs up to 1e9.
So we need an efficient sorting algorithm: O(N log N) using standard sorting with a custom comparator.
But we need to consider memory: N up to 1e6, each record is two integers, so about 8 bytes each (if using 32-bit ints, 4+4=8) or 16 bytes for typical language overhead. Manageable.
We need to read input, sort, output.
The problem statement seems straightforward.
But we need to pay attention to edge cases: multiple countries, many entries.
We should implement in a language like C++ with fast I/O, or Python with sys.stdin.buffer and sys.stdout.write.
Given that N can be up to 1,000,006 (1000006) in Python, sorting 1e6 items with custom key may be okay. Use sort with key = (country, -runs). That sorts ascending by country and ascending by -runs (which yields descending runs). That's easy.
But careful: runs up to 1e9, -runs is negative but that's fine.
Alternatively, we can use sort with comparator lambda: x[0] != y[0] compare countries else compare runs descending.
In Python, we can do: sorted(list, key=lambda x: (x[0], -x[1])). This is efficient.
But we need to read input fast: sys.stdin.buffer.read().split() and parse ints.
We output each pair.
Now let's think about constraints: N up to 1e6, input lines ~2*1e6 integers. That's fine.
But the problem might be from some competitive programming where they require C++ but Python should be okay if optimized.
However, the sample input and output provided:
5
4 12
2 1
3 10
2 7
2 5
Output:
2 7
2 5
2 1
3 10
4 12
Yes.
Now, double-check the description: They want country codes sorted ascending, and runs for each country descending. That's what we do.
Thus the solution is trivial.
But perhaps there is a twist? The story mentions Virat likes ascending, Maxwell descending, they compromised to sort countries ascending and within country runs descending. So that's it.
Implementation details:
Pseudo:
def main():
import sys
data = sys.stdin.buffer.read().split()
if not data: return
it = iter(data)
n = int(next(it))
pairs = []
for i in range(n):
country = int(next(it))
runs = int(next(it))
pairs.append((country, runs))
# sort
pairs.sort(key=lambda x: (x[0], -x[1]))
out = []
for c, r in pairs:
out.append(f"{c} {r}")
sys.stdout.write("\n".join(out))
if name == "main":
main()
That should work.
But we must consider that N can be as large as 1,000,006, reading everything into a list of tuples may use memory: each tuple overhead is ~56 bytes plus ints 28 bytes each (in CPython). That might be ~100 bytes per record => 100 MB for 1e6, borderline but might be okay for typical memory limits (512 MB). But could be close. However, we can reduce memory by using array of ints or using numpy? But typical CP contest memory limit is maybe 256 MB. 1e6 records in Python using list of tuples could exceed.
Let's estimate: In Python 3.9, a tuple of two ints: each int object is 28 bytes (or 24 on 64-bit? Actually Python int is 28 bytes on 64-bit CPython 3.8+ due to base 28? Let's recall: PyObject overhead 16 bytes, then the value (8 bytes) plus some? Actually CPython int is 28 bytes for 64-bit. So two ints: 228=56. Tuple object: 56 bytes (including 2 pointers)? Actually a tuple of length 2 has overhead: PyTupleObject includes an array of PyObject of size 2. The total size for the tuple object: 56 bytes (size of PyTupleObject for 2 items). So combined, ints + tuple = 56 + 56 = 112 bytes? But the tuple holds references to the ints, not including the ints themselves. The ints are separate objects, so total memory per pair could be around 56 (tuple) + 228 = 112 bytes. Plus the list holds references to the tuple objects (8 bytes per reference). The list itself holds pointers to each tuple, so additional 8 N = 8 MB. So total ~112 MB + 8 MB = 120 MB for 1e6. That's okay for 256 MB limit. But if memory limit is tighter (like 128 MB), it might exceed.
Alternatively, we could use a more memory efficient representation: we could store in two parallel lists, but sorting would be tricky. Better to use an array of structured types via Python's built-in array module? Or use list of pairs but with small ints? However, runs up to 1e9, so still int objects.
Alternatively, we can use the sort key method that reads from file, but we need to sort; we could use an external sort? Probably not needed.
Given typical constraints on CodeChef or similar, Python with 1e6 ints might be okay.
We can also use the trick: use list of tuples and sort. Many CP solutions in Python for N up to 10^6 with two ints are accepted.
But careful: N can be up to 1000006 exactly? Possibly they set it to 1,000,006 to be slightly above 1e6? But that's similar.
So the solution is fine.
Now, we need to output sorted list.
That's the answer.
But wait, the problem statement says "Virat likes arranging stuffs in ascending order and wants the data list to be sorted in ascending order, while Maxwell prefers descending order. As they are good friends, they mutually agreed that country codes should be sorted in ascending order and for each country code, the runs of all the batsmen for that particular country should be sorted in descending order."
Thus the comparator is as described.
Edge Cases:
- If there is only one country, just sort runs descending.
- If runs are equal for same country, order doesn't matter, but descending order will keep them as is (stable? not needed). So fine.
Implementation details:
We'll use Python for demonstration, but could also use C++.
But as ChatGPT, we need to produce a final answer: Provide a solution description, algorithm, complexity analysis, and code.
Thus produce answer.
But maybe they want a solution that groups by country and then sorts runs descending and outputs in country ascending order. Could also be solved with dictionary mapping country to list of runs, then sort keys and sort each list descending and output. That could be more efficient memory wise? Because storing runs as lists might reduce overhead? Possibly similar.
Using dictionary: for each country, we accumulate list of runs. Then sort the keys and for each key sort runs descending and output. The total memory for runs as int objects is the same. But we have additional dictionary overhead per key. But could be similar. The sorting approach with a single list is simpler.
Either is fine.
But for large N, dictionary may be faster for grouping? But we don't need grouping; we just need to sort by (country asc, runs desc). That's directly done by sorting the list.
However, there is a nuance: if we have many distinct countries, sorting with key (country, -runs) is fine.
Thus final answer: read N, read N pairs, sort with key lambda (c, -r), output.
Time: O(N log N), Space: O(N).
Now provide code.
We must use sys.stdin.buffer for fast input.
Edge: N may be 0? But N>=1.
We'll test with sample.
Also note: Input might have trailing spaces, but reading split works.
Potential integer overflow? Not in Python.
Alright.
I'll now produce final answer: explanation, algorithm, complexity, and Python code.
```python
import sys
def main() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
it = iter(data)
n = int(next(it))
# Read all batsmen data
batsmen = []
for _ in range(n):
country = int(next(it))
runs = int(next(it))
batsmen.append((country, runs))
# Sort: country ascending, runs descending
batsmen.sort(key=lambda x: (x[0], -x[1]))
# Fast output
out_lines = [f"{c} {r}" for c, r in batsmen]
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()