Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.
Assistant
We need to sort the exams
Then greedily choose the earliest valid date for each exam
The date chosen must be >= the previous chosen date
Sorting strategy:
Sort by scheduled date (ai) first
If two exams have the same ai, sort by bi
Then for each exam in sorted order:
If bi >= current_day, take it on bi
Otherwise, take it on ai
Let me verify with an example.
I notice you've provided the input format for what appears to be a competitive programming problem, but the actual problem statement and question are missing.
From the format given, this looks like it could be Codeforces Problem 479C - "Exams" or a similar scheduling problem. Let me provide a solution assuming this is the standard problem:
Problem Interpretation
Assumed Problem: Vasiliy has n exams. Each exam has:
ai = scheduled/exam date
bi = early registration date (where bi < ai)
He must take each exam either on the early date bi or the scheduled date ai. He wants to take exams as early as possible while ensuring the dates are non-decreasing.
Solution
Algorithm:
Sort exams by scheduled date ai, then by early date bi (both ascending)
Greedily select dates: Keep track of current_day. For each exam:
If bi ≥ current_day: take exam on bi
Else: take exam on ai
Output all selected dates
Python Implementation:
python
n = int(input())
exams = []
for i in range(n):
ai, bi = map(int, input().split())
exams.append((ai, bi, i)) # store original index for output
# Sort by ai, then by bi
exams.sort(key=lambda x: (x[0], x[1]))
result = [0] * n
current_day = 0
for ai, bi, idx in exams:
if bi >= current_day:
current_day = bi
else:
current_day = ai
result[idx] = current_day
# If output needs to be in sorted order:
for d in sorted(result):
print(d)
Verification with Example:
Input:
code
5
5 2
3 1
4 2
5 3
6 1
Sorted exams: (3,1), (4,2), (5,2), (5,3), (6,1)
Exam
ai
bi
current_day before
Choice
current_day after
1
3
1
0
bi=1 (1≥0)
1
2
4
2
1
bi=2 (2≥1)
2
3
5
2
2
bi=2 (2≥2)
2
4
5
3
2
bi=3 (3≥2)
3
5
6
1
3
ai=6 (1<3)
6
Output: 1, 2, 2, 3, 6
Could you please confirm this is the correct problem, or provide the complete problem statement if it's different? The constraints and exact output format would help me give a more precise solution.