H. Jet Lag
47th Annual hosted by ICPC World Championship AASTMT Problem H Jet Lag Time limit: 2 seconds The ICPC...
Problem Statement
Formatted from the contest statement text, with sample tests broken out into copyable blocks.
47th Annual hosted by
ICPC World
Championship AASTMT
Problem H
Jet Lag
Time limit: 2 seconds
The ICPC World Finals are here and they are packed full of activities you want to attend — speeches,
presentations, fun events, not to mention the contest itself. There is only one problem: when are you
going to sleep?
When you fall asleep, you always set a timer because otherwise you would be able to sleep forever.
Using the timer, you can choose to sleep for any positive integer amount of minutes. After sleeping for
k minutes, you will be rested for another k minutes (and so you will not be able to fall asleep again);
and then you will be able to function for a third k minutes (so you can stay awake, but you can also go
to sleep if you want to).
You know the times of all the activities at the Finals; you should plan your sleep schedule to not miss
any part of any event. Just before the Finals start (at minute 0), you will arrive in your hotel room after
a long journey and you will need to sleep immediately.Input
The first line of input contains a positive integer n (1 ≤ n ≤ 200 000), the number of activities planned for the Finals. The ith of the remaining n lines contains two positive integers bi and ei (bi < ei , ei ≤ bi+1 , 0 ≤ b1 , en ≤ 1010 ), the beginning and end time of the activity, counted in minutes from the beginning of the Finals.
Output
If it is possible to find a sleep schedule that allows you to participate in all planned activities in their entirety, then output such a schedule in the format described below. Otherwise, output impossible. A sleep schedule is specified by a line containing the number p (1 ≤ p ≤ 106 ) of sleep periods, followed by p lines. The ith of these lines contains two integers si and ti — the beginning and end time of the ith sleep period, counted in minutes from the beginning of the Finals. Note that you should not output any sleep period after the last activity. The sleep periods must satisfy 0 = s1 < t1 < s2 < t2 < . . . < tp ≤ bn as well as the condition described in the statement that does not allow you to fall asleep for some time after a sleep period. You may fall asleep immediately after an activity (so it may be that si = ej ) and you may wake up just before an activity (so it may be that ti = bj ). If there are multiple valid sleep schedules, any one will be accepted. It can be shown that if there is a valid sleep schedule, then there is also one with at most 106 sleep periods.
World Finals | ICPC 2023 Luxor
47th Annual hosted by
ICPC World
Championship AASTMTSample Tests
3
30 45
60 90
120 180 2
0 30
90 120 1
0 60 impossible 7
31 32
35 41
48 55
69 91
1000 2022
2022 2023
2994 4096 5
0 5
10 28
56 68
92 900
2025 2900 Editorial
The solution write-up is rendered from the LaTeX source, with equations kept live through MathJax.
Key Observations
Ignoring the ``cannot sleep while rested'' restriction for a moment, the only useful sleep intervals are the free gaps \[ [e_{i-1}, b_i], \qquad e_0 = 0. \] Sleeping any shorter interval inside the same gap can only decrease how far we can stay functional.
If we sleep through a whole gap $[l,r]$, then the sleep lasts $r-l$ minutes, so afterwards we can stay functional until \[ r + 2(r-l) = 3r - 2l. \]
Thus, in the relaxed model, each gap acts like a transition $l \mapsto 3r-2l$. Scanning the gaps in time order and keeping every gap that increases the current reachable time computes the farthest reachable time.
After this greedy selection, consecutive chosen sleeps can be shortened slightly so that the next sleep starts after the previous rested phase ends, while still keeping the next chosen start reachable.
Algorithm
If the first activity starts at time $0$, output
impossible, because we are required to sleep immediately.Let the free gaps be $[l_i,r_i]=[e_{i-1},b_i]$.
Scan them from left to right, maintaining the farthest reachable time
reachin the relaxed model:if $l_i > \texttt{reach}$, the gap is unreachable;
otherwise sleeping through it would extend reach to $3r_i-2l_i$;
if this extension is larger than the current
reach, keep the gap and updatereach.If the final
reachis smaller than the end of the last activity, outputimpossible.Otherwise let the chosen relaxed sleeps be $[s_i,t_i]$. For each consecutive pair, shorten the earlier interval to \[ t_i \leftarrow \min\!\left(t_i,\ \left\lfloor\frac{s_i+s_{i+1}}{2}\right\rfloor\right). \] Keep the last chosen interval unchanged.
Output the shortened intervals. enumerate
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
After processing the first $i$ gaps, the variable
reachequals the farthest time reachable in the relaxed model using only those gaps.Proof.
We use induction on $i$. Initially
reach$=0$, which is optimal before using any gap.Consider gap $[l_i,r_i]$. If $l_i > \texttt{reach}$, then this gap cannot be used by any schedule based on the first $i$ gaps, so the optimum does not change. Otherwise the gap is reachable, and using it completely extends the reachable time to $3r_i-2l_i$. Any relaxed schedule using only the first $i$ gaps either skips this gap or uses it that way, so the new optimum is exactly \[ \max(\texttt{reach},\, 3r_i-2l_i), \] which is the algorithm's update. □
Lemma 2.
If the relaxed greedy phase reaches the end of the last activity, then the shortening step produces a valid sleep schedule for the original problem.
Proof.
Consider two consecutive chosen starts $s_i < s_{i+1}$. After shortening, we have \[ t_i \le \frac{s_i+s_{i+1}}{2}, \] so \[ 2t_i-s_i \le s_{i+1}. \] Thus the next sleep begins no earlier than the end of the previous rested phase.
At the same time, the original relaxed choice guaranteed that the next chosen start was reachable. Since \[ \frac{s_i+s_{i+1}}{2} \ge \frac{2s_i+s_{i+1}}{3}, \] the shortened endpoint still satisfies \[ 3t_i-2s_i \ge s_{i+1}, \] so we can remain awake until the next chosen sleep begins. Therefore every shortened interval is valid, and the last one still reaches far enough. □
Theorem.
The algorithm outputs a valid schedule whenever one exists, and outputs
impossibleotherwise.Proof.
By Lemma 1, the greedy scan computes the maximum reachable time in the relaxed model. If even that does not reach the end of the last activity, then no valid schedule exists in the original problem either. If it does reach far enough, Lemma 2 converts the relaxed schedule into a valid original schedule. Therefore the algorithm is correct. □
Complexity Analysis
We scan the activities once and store the chosen intervals. The running time is $O(n)$ and the memory usage is $O(n)$.
Implementation Notes
All times fit in 64-bit signed integers.
The implementation verifies after shortening that each interval still has positive length and still reaches the next chosen start.
Code
C++ solution used for this page.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
int n;
cin >> n;
vector<long long> b(n), e(n);
for (int i = 0; i < n; ++i) {
cin >> b[i] >> e[i];
}
if (b[0] == 0) {
cout << "impossible\n";
return;
}
vector<pair<long long, long long>> chosen;
long long reach = 0;
for (int i = 0; i < n; ++i) {
long long left = (i == 0 ? 0 : e[i - 1]);
long long right = b[i];
if (right <= left) {
continue;
}
if (left > reach) {
break;
}
long long extension = 3 * right - 2 * left;
if (extension > reach) {
chosen.push_back({left, right});
reach = extension;
}
}
if (chosen.empty() || reach < e.back()) {
cout << "impossible\n";
return;
}
for (int i = 0; i + 1 < static_cast<int>(chosen.size()); ++i) {
long long limit = (chosen[i].first + chosen[i + 1].first) / 2;
chosen[i].second = min(chosen[i].second, limit);
}
for (int i = 0; i < static_cast<int>(chosen.size()); ++i) {
long long s = chosen[i].first;
long long t = chosen[i].second;
if (t <= s) {
cout << "impossible\n";
return;
}
if (i + 1 < static_cast<int>(chosen.size())) {
long long next_start = chosen[i + 1].first;
if (next_start < 2 * t - s || 3 * t - 2 * s < next_start) {
cout << "impossible\n";
return;
}
} else if (3 * t - 2 * s < e.back()) {
cout << "impossible\n";
return;
}
}
cout << chosen.size() << '\n';
for (const auto& interval : chosen) {
cout << interval.first << ' ' << interval.second << '\n';
}
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files and Assets
Raw files are still available here when you want the original TeX, C++, or statement assets.
Show raw files
competitive_programming/icpc/2023/H-jet-lag/solution.texC++ implementationcompetitive_programming/icpc/2023/H-jet-lag/solution.cppStatement textcompetitive_programming/icpc/2023/H-jet-lag/statement.txtStatement PDFcompetitive_programming/icpc/2023/H-jet-lag/statement.pdfMetadatacompetitive_programming/icpc/2023/H-jet-lag/meta.jsonYear packetcompetitive_programming/icpc/2023/contest_problems.pdf