1449. When the Hall Is Fullest
n guests are given. Guest i is present over the CLOSED stretch [a, b]
with a <= b: present from moment a to moment b INCLUSIVE. A guest leaving
at moment t and a guest arriving at moment t are both present at t.
a == b is allowed and means a guest present for a single moment.
Print two integers on one line: the largest number of guests present at once, and the EARLIEST moment at which that many are present.
[1,5] [2,6] [8,9] [3,4] -> 3 3
[1,3] [3,5] [5,7] -> 2 3
[7,7] -> 1 7
The middle line is the convention. Guests one and two are both present at
moment 3 and guests two and three are both present at moment 5, so the fullest
the hall gets is 2, first at moment 3. Under a HALF-OPEN reading those three
never meet and the answer would be 1 1.
## What most people try first
Asking, for each moment, how many guests are present. That is correct and there
are up to 2 * 10^9 moments, because coordinates reach 10^9 in both
directions.
The second reflex is to reuse the sweep from problem 1448 unchanged. It gives the wrong maximum here, by exactly the number of handovers at the peak, and it gives no moment at all.
## Sweep, but read the count in the right place
for each guest [a, b]: event(a, ARRIVE) and event(b, LEAVE)
sort by coordinate; at equal coordinates put every ARRIVE BEFORE every LEAVE
count = 0
for each distinct coordinate x, in order:
apply every ARRIVE at x count goes up
if count > best: best = count; moment = x
apply every LEAVE at x count goes down
Reading the count between the arrivals and the departures at x is what makes
it the number present AT x under the closed reading. O(n log n).
## Why the earliest moment is always an arrival coordinate
The count only ever goes UP at an arrival. So the first moment at which a new
record is reached is a moment at which somebody arrives, and that moment is
one of the a values. Checking only the arrival coordinates is therefore
enough, and reporting the first coordinate that sets a new record, using a
strict > rather than >=, gives the earliest one.
Using >= would report the LAST moment holding the maximum instead of the
first, which is a different and equally reasonable question and not the one
asked.
## The boundary that breaks a careless implementation
a == b is legal. A guest present for a single moment produces an ARRIVE and
a LEAVE at the same coordinate, and the order above counts them: the arrival
lands first, the count is read, then the departure. A sweep that applies both
deltas before reading misses that guest entirely and reports 0 for a hall
holding one.
## Overflow
The count is at most n = 200000. The moment printed is a coordinate, at most
10^9 in magnitude, inside the 2147483647 a signed 32 bit integer holds.
Nothing accumulates, so 32 bit arithmetic is enough throughout.
## Related
Problem 1448 is the same sweep under the half-open reading, where a close is processed before an open. Problem 277 counts platforms under this closed reading but takes arrivals and departures as two unrelated sorted lists and prints only the count. Problem 1459 prints the two conventions' answers together, and problem 1479 reports the witnessing moment under the half-open reading.
Constraints - `1 ≤ n ≤ 200000` - `-10^9 ≤ a ≤ b ≤ 10^9` - each guest is present over the closed stretch `[a, b]`, so arriving exactly as another leaves still counts as both present - `a == b` is allowed and means a guest present for one moment
Input
The first line contains the integer n.
Each of the next n lines contains two integers a and b, one guest, in no particular order.
Output
Print two space-separated integers on one line: the largest number of guests present at the same moment, then the earliest moment at which that many are present. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.