We are given n events, each with an interval [li, ri] inclusive. We need to assign a distinct integer date di to each event, such that di ∈ [li, ri] and all di are distinct. At most one event per day. We need to output any valid assignment.
Constraints: n ≤ 100, li, ri up to 10^7. So intervals are large but number of events is small.
The problem is a classic assignment problem with intervals. Since n is small, we can do greedy: sort events by increasing ri (or maybe by li) and then assign the smallest available date that lies in the interval. But careful: we need to ensure distinctness. A standard approach: sort events by ri (end date) ascending, then for each event assign the smallest available date >= li that hasn't been used yet. Since the intervals are sorted by end, this greedy works for interval scheduling / assignment to distinct points, similar to "maximum matching" in bipartite graph with intervals as left nodes and dates as right nodes. Actually, this is equivalent to matching each interval to a distinct integer point within it. The greedy algorithm of sorting by right endpoint and then assigning the smallest available feasible date is known to work if possible (by Hall's marriage theorem). It's like scheduling as many tasks as possible, but here we need all tasks assigned. Since it's guaranteed possible, it should work.
Alternatively, we can also sort by li ascending and assign each event the smallest free date >= li, but need to be careful because if we assign too early, we might block later events with narrow intervals. For example: intervals [1,1], [1,2]. If we assign event1: date1=1, event2: smallest free >=1 is 2, works. But if intervals [1,2], [1,1] and we sort by li, we assign first event [1,2] date1=1, then event2 [1,1] would have no free date. So we need to sort by ri to prioritize narrow intervals. The greedy of sorting by ri and then assigning smallest available date >= li works.
Proof: The problem reduces to matching intervals to points. The greedy that sorts intervals by right endpoint and then for each interval picks the smallest available point not less than its left endpoint is a standard algorithm for finding a system of distinct representatives for intervals (see "interval scheduling with unit tasks").
We have n ≤ 100, so we can implement straightforward.
Algorithm:
-
Read n.
-
Store list of (li, ri, original_index) because we need to output in the order of input? The problem statement says "Print n numbers — the dates on which the events took place." Typically, the output order corresponds to the input order (i.e., the i-th printed number is the date for i-th event). So we need to output in original order. We can assign dates and then reorder.
-
Sort events by ri ascending, breaking ties maybe by li ascending? Not necessary but fine.
-
Maintain a set (or boolean array) of used dates. Since n is only 100 but dates up to 10^7, we can just store used dates in a set (e.g., Python set) and iterate to find smallest free date >= li.
Finding smallest free date: We could loop from li upwards and check if the date is in used set. Since n is small (max 100) and each search could in worst-case go up to ri - li + 1 (which could be up to 10^7) but we have only 100 events and each event we might scan many numbers? That could be too slow if intervals are large. But we can optimize: Because we only have at most 100 used dates, we can simply try dates from li to ri and find the first not in set. Since set lookup O(1), scanning up to maybe ri - li + 1 steps could be huge (10^7) in worst-case per event, and with 100 events that could be 10^9 operations, which is too slow.
We need a smarter approach. But we can use a union-find "next available" structure that for each date we know the next free date. However, dates up to 10^7, we cannot allocate array of size 10^7? That is 10 million, which is fine memory-wise (10 million integers would be about 40 MB in C++ or Java, but in Python it might be heavy but maybe okay? 10 million ints in Python is huge (28 bytes each), that's 280 MB, too much). So we need a different approach.
Alternative: Since n is only 100, we can do a matching using bipartite graph? There are n events and potential dates? Dates could be up to 10^7, so we can't enumerate all dates. However, we can consider that the only important dates are the boundaries of intervals? Actually, for each event, we need to pick one date. The classic solution: sort by ri, then maintain a variable "current date". For each event in sorted order, assign the max of li and the next free date? Wait, there is a known greedy for "assign distinct numbers within intervals" that uses a pointer and increments it. Let's think:
If we sort events by li (or by ri) and we keep a variable cur = 1. For each event in sorted order, we assign date = max(li, cur). But that doesn't guarantee di ≤ ri. So we need to adjust.
Better: sort by li ascending. For each event, assign the smallest integer >= li that is not used. To find that quickly, we can maintain a variable "cur" that increases globally? But the intervals may have different li.
Observation: The set of used dates will be some numbers. Since we only have 100 dates, we can do a more efficient search by using sorted list of used dates. For each event, we want the smallest number x >= li such that x not in used set. Given that used set size is small, we can check candidate = li, then if it's used, we increment candidate and check again. But incrementing one by one could be large if there is a long block of used dates? But the used set size is at most 100, so there can be at most 100 used numbers. The worst-case scenario: we have 100 used dates tightly packed, e.g., 1..100 all used. Then for an event with li=1, we would need to scan from 1 to 101 to find free, that's 101 checks, which is fine. However, it's not about scanning many numbers because used set is sparse? Actually, the used set could be scattered, but the candidate may need to skip many numbers that are not used? If candidate is li, we check if it's in used set. If it's not, we assign it. If it is used, we increment candidate by 1. But incrementing one by one, we might need to check many numbers that are not used? But if they are not used, then we would assign the first one that is not used. So we only need to check numbers until we find a free one. However, the free one could be far away if there is a long contiguous block of used dates. Since the total used dates is at most 100, the longest possible block of used dates could be 100 (if they are consecutive). So scanning from li might at most go through 100 used dates plus maybe some unused ones? Actually, if we have a block of 100 consecutive used numbers from L to L+99, and li = L, then we check L (used), L+1 (used), ..., L+99 (used), L+100 (free). That's 101 checks. So each assignment scanning worst-case O(n) steps, total O(n^2) = 10k, trivial.
But wait, the intervals can be huge, but that doesn't affect the scanning because we are scanning candidate starting from li and increasing one by one. The number of steps is not dependent on the size of the interval, but rather on the number of used dates we skip. However, there is a nuance: if li is very small and the used set is sparse but far away? Actually, we only check numbers that are <= ri eventually. Suppose we have many used dates, but they are far apart. For example, used dates: 1, 100, 200, 300,... up to 100 numbers. For an event with li=1, we check 1 (used), then 2 (free), so only 2 checks. So it's fine. The worst-case indeed is when used dates form a consecutive block of size up to n. So scanning is O(n) per event.
But is there any scenario where we might need to scan beyond ri? If we can't find a free date within [li, ri], the algorithm would fail, but the problem guarantees existence. So we will always find one before ri. So scanning at most (number of used dates within the interval) +1 steps.
Thus, naive scanning per event from li upwards, checking membership in a set, is efficient given n <= 100. Implementation is simple.
But we need to be careful: we must sort events by ri, not by li. If we sort by li and do greedy assign smallest free date >= li, is it always correct? Let's test: Example: [1,2], [1,1]. If sorted by li (both li=1), order could be [1,1] first then [1,2] -> assign date1=1 for event1, then event2 smallest free >=1 is 2, works. But if order is [1,2] first then [1,1] -> assign date1=1 for event1, then event2 can't assign, fails. Sorting by li doesn't guarantee any order among equal li, so we could get the wrong order. But if we sort by li and also by ri (ascending ri when li equal) maybe? Let's test: [1,2], [1,1] sorted by li then ri: [1,1] first, [1,2] second, works. What about [1,3], [2,2]? Sorting by li then ri: [1,3] first, [2,2] second. Then assign: event1: assign smallest free >=1 is 1. event2: smallest free >=2 is 2 (since 2 not used), works. Actually, [2,2] can take 2. Good. Another problematic: [1,2], [2,2]? Sorted by li: [1,2] then [2,2]. event1 assign 1, event2 assign 2 works. [1,1], [2,2] works. Let's find a counterexample where sorting by li fails even if we sort by ri within li. Classic example from assignment problem: intervals [1,3], [2,2]. Sorted by li: [1,3] (li=1) then [2,2] (li=2). Assign first: assign 1. Second: assign 2 -> works. So not counterexample. How about intervals: [1,2], [2,3], [2,2]? Sorted by li then ri: [1,2] (li=1, ri=2), [2,2] (li=2, ri=2), [2,3] (li=2, ri=3). Assign: first: assign 1. second: assign 2 (since free). third: assign smallest free >=2 is 3 (since 2 used) which is <=3, works. So sorting by li might actually work if we break ties by ri? But is there a known counterexample? Let's think about interval scheduling: For assigning distinct numbers to intervals, the greedy by earliest deadline (right endpoint) works. Greedy by left endpoint might fail sometimes? I recall that to find a system of distinct representatives for intervals, the greedy algorithm that sorts intervals by right endpoint and then picks the leftmost available point works. Sorting by left endpoint is not always correct. For example, consider intervals: [1,1], [1,2], [2,2]. If sorted by left endpoint (and then right maybe): [1,1], [1,2], [2,2]. Assign: event1: 1; event2: smallest free >=1 is 2 (works); event3: smallest free >=2 is ? 2 is used, so 3 not in [2,2] -> fail. But is there a feasible assignment? Yes: event1:1, event2:? Actually, we need three distinct dates. Intervals: event1 [1,1] must be date 1. event2 [1,2] can be 2. event3 [2,2] must be 2, but 2 is taken, so impossible. Wait, it's impossible because two intervals both require 2 exclusively? Let's check: events: 1: [1,1], 2: [1,2], 3: [2,2]. Is there a valid assignment? Event1 must be 1. Event3 must be 2. Event2 can be either 1 or 2, but both are taken, so no. So it's actually impossible. So not a counterexample.
What about intervals: [1,2], [1,2], [1,2]? That is possible (assign 1,2,? but only 1 and 2 available, third event needs date in [1,2] but both taken, impossible unless n=2). So guarantee of existence ensures a solution.
I need a clear example where sorting by left endpoint fails. Suppose intervals: [1,3], [2,2], [2,3]. Sorted by li: [1,3] (li=1), [2,2] (li=2), [2,3] (li=2). Assign: [1,3] assign 1; [2,2] assign 2; [2,3] assign smallest free >=2 is 3 (since 2 used) works. So okay.
Let's search: Known result: The greedy algorithm for "interval graph coloring" or "minimum number of resources" sorts by start time. But here we need to assign distinct points. Actually, it's the "matching of intervals to points" problem. There is a theorem: a family of intervals has a system of distinct representatives if and only if the family satisfies Hall's condition, which is equivalent to for any k intervals, the union of the earliest k left endpoints? I'm not sure. The standard algorithm: sort intervals by right endpoint, then for each interval assign the smallest integer that is within the interval and not used. That's proven optimal.
But can we do sorting by left endpoint? Let's attempt to construct a counterexample manually. We need intervals such that if we assign greedily from the smallest left, we may assign a date that later blocks an interval that has a small right endpoint but larger left. For the algorithm to fail, we must have an interval with earlier left but later right, and a later interval with later left but earlier right. Greedy by left picks the earliest left interval and assigns the smallest possible number, which could be a number that the later interval (with earlier right) needed to use because its window is narrow. For example: Intervals: A = [1,100], B = [2,2]. If we sort by left: A then B. Assign A: assign 1 (since smallest free >=1). B: assign 2 (free), works. Not failure because B's narrow window is 2, which is still free.
What if A = [1,2], B = [2,2]? Assign A: 1, B: 2 works.
What about A = [1,3], B = [2,2]? A: 1, B:2 works.
A = [1,3], B = [3,3]? A: 1, B:3 works? B's left=3, assign smallest free >=3 is 3, works.
A = [2,3], B = [1,2]? Sorting by left: B first (li=1), then A. B: assign 1, A: assign smallest free >=2 is 2 (since 2 not used), works. Both have representation.
We need B to have a very narrow interval that forces it to take the leftmost date that A might take, but A's left is smaller so A gets it first. For B to be forced to take a specific number that A could take, B's interval must be exactly that number. For instance, A = [1,2], B = [1,1]. Sort by left: which order? li equal, if we sort by ri, B [1,1] then A [1,2] works. If we sort only by li (no secondary), we could have A first then B and fail. But we can impose a secondary sort by ri to ensure narrow intervals come first. So if we sort by li ascending, then ri ascending, does it guarantee correctness? Consider intervals: A = [1,3], B = [2,2], C = [2,3]. Sorted by li then ri: A (1,3), B (2,2), C (2,3). Already we saw it works. Might there be a scenario where even sorting by li then ri fails? Let's try: intervals: A = [1,3], B = [2,2], D = [2,3]? Already considered. How about more intervals: A = [1,4], B = [2,2], C = [3,3], D = [3,4]. Sorted: A (1,4), B (2,2), C (3,3), D (3,4). Assign: A:1, B:2, C:3, D:? smallest free >=3 is 4 -> works. Good.
Consider intervals: A=[1,5], B=[2,3], C=[4,4]. Sorted: A, B, C. Assign: A:1, B:2, C:4? Wait B:2, but could also be 3. But assign smallest free >=2:2. C: smallest free >=4:4. Works.
It seems that sorting by left and then right might actually work, but I'm not entirely convinced. I recall that for interval scheduling (selecting maximum number of non-overlapping intervals), sorting by right endpoint works. For assigning distinct points to intervals, the typical solution sorts by right endpoint. Let's try to find a counterexample with sorting by left endpoint (with ties by right) using a systematic approach.
We need a case where greedy by left fails but a solution exists. Consider intervals:
I1: [1, 2]
I2: [1, 3]
I3: [2, 2]
We sort by li then ri: I1 (1,2), I2 (1,3), I3 (2,2). Greedy: I1 assign 1; I2 assign smallest free >=1 -> 2 (since 1 used) works; I3 assign smallest free >=2 -> but 2 used, next 3 not in [2,2] -> fail. However, does a solution exist? Let's see: Need distinct dates: I1 can take 1 or 2; I2 can take 1,2,3; I3 can take 2. We need to assign all three distinct numbers, but only numbers 1,2,3 are available. I3 must take 2. So I1 must take 1, I2 takes 3. That's valid: I1=1, I2=3, I3=2. So there is a solution. Now check greedy by left: I1=1, I2=2, then I3 cannot get 2. So fails. But our sorting order was I1 then I2 then I3. However, note that with sorting by li then ri, I1 and I2 have same li=1, so we sort by ri: I1 (ri=2) before I2 (ri=3). That's what we did. But that ordering caused I1 to take 1, I2 to take 2, blocking I3. If we had sorted by li but with a different tie-breaking (like by ri descending?), maybe I2 first then I1: I2 assign 1, I1 assign 2, I3 assign ? 2 is used, so still fails. Actually, try I2 first: assign 1; I1 assign smallest free >=1 -> 2; I3 assign smallest free >=2 -> 2 used -> fail. So sorting by li fails regardless of tie-breaking? Let's try tie-breaking by ri descending: I2 (1,3) first, assign 1; I1 (1,2) next, assign 2; I3 (2,2) fails. So fails. Could we sort differently? Perhaps we need to sort by li then something else? In this example, the greedy by left fails because I3 has later left (2) but narrow interval; to satisfy I3, we must assign I2 a date >2 (i.e., 3). But greedy by left will assign I1 and I2 before I3, and they might consume the only date (2) that I3 needs. However, is there any order that would succeed? If we process I3 before I2? But sorting by left ensures I1 and I2 have li=1 <=2, so they will come before I3 if we sort by li. So I3 will always be processed after them. So greedy by left cannot recover because when it gets to I3, the crucial date 2 might have been taken. So greedy by left (even with tie-breaking) fails for this instance.
Thus, we need to sort by right endpoint (or at least process intervals in order of increasing right endpoint) to ensure that intervals with earlier deadlines get assigned first. That's the standard approach.
Now, check the instance with sorting by ri: intervals sorted by ri: I1 (1,2) ri=2, I3 (2,2) ri=2, I2 (1,3) ri=3. For ties on ri, perhaps sort by li? For I1 and I3 both ri=2, order can be I1 then I3 or I3 then I1. Let's test both:
Case A: I1 first, assign smallest free >=1 -> 1. Then I3 assign smallest free >=2 -> 2. Then I2 assign smallest free >=1 -> 1? 1 used, 2 used, next 3 (within [1,3]) -> 3. Works.
Case B: I3 first, assign smallest free >=2 -> 2. Then I1 assign smallest free >=1 -> 1. Then I2 assign smallest free >=1 -> 1 used, 2 used, next 3 -> works. So sorting by ri works regardless of tie-breaking? It may need to assign smallest free >= li; but what if tie-breaking order matters? Possibly not, but we need to ensure that picking the smallest free date works. For intervals with same ri, either order should be fine. Let's try to find a counterexample where sorting by ri fails. Suppose intervals: A=[1,2], B=[1,1]. Sorted by ri: B (ri=1) then A (ri=2). B assign smallest free >=1 -> 1. A assign smallest free >=1 -> 2 (since 1 used) works. Good.
Thus, safe approach: sort by ri, then assign.
Therefore, algorithm: sort events by ri ascending (if ri equal, any order). Then iterate. For each event, find the smallest integer d such that d >= li, d <= ri, and d not used. Since n is small, we can just start at li and increment while d is used and d <= ri. If we exceed ri, that would mean no date available, but guarantee exists. But we must ensure that we do not infinite loop; we will break when found.
Implementation details:
- Read n.
- events = list of (l, r, index)
- Sort by r.
- used = set()
-