All ICPC entries
Competitive Programming

ICPC 2023 - H. Jet Lag

We must choose sleep intervals so that no activity is missed. Sleeping for k minutes gives k more minutes during which we are rested and cannot fall asleep again, followed by another k minutes during which we may stay...

Source sync Apr 19, 2026
Track ICPC
Year 2023
Files TeX, C++, statement assets
Folder competitive_programming/icpc/2023/H-jet-lag
ICPC2023TeXC++statement textstatement pdf

Source-first archive entry

This page is built from the copied files in competitive_programming/icpc/2023/H-jet-lag. Edit competitive_programming/icpc/2023/H-jet-lag/solution.tex to update the written solution and competitive_programming/icpc/2023/H-jet-lag/solution.cpp to update the implementation.

The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.

Problem Statement

Copied statement text kept beside the solution archive for direct reference.

World Finals | ICPC 2023 Luxor
     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.

47th ICPC World Championship Problem H: Jet Lag © ICPC Foundation                                         15

                                 World Finals | ICPC 2023 Luxor
   47th Annual                                                      hosted by
    ICPC World
  Championship                                                      AASTMT

Sample Input 1                                Sample Output 1
3                                             2
30 45                                         0 30
60 90                                         90 120
120 180

Sample Input 2                                Sample Output 2
1                                             impossible
0 60

Sample Input 3                                Sample Output 3
7                                             5
31 32                                         0 5
35 41                                         10 28
48 55                                         56 68
69 91                                         92 900
1000 2022                                     2025 2900
2022 2023
2994 4096

47th ICPC World Championship Problem H: Jet Lag © ICPC Foundation               16

Editorial

Rendered from the copied solution.tex file. The original TeX source remains available below.

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

  1. If the first activity starts at time $0$, output impossible, because we are required to sleep immediately.

  2. Let the free gaps be $[l_i,r_i]=[e_{i-1},b_i]$.

  3. Scan them from left to right, maintaining the farthest reachable time reach in 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 update reach.

    • If the final reach is smaller than the end of the last activity, output impossible.

    • 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 reach equals 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 impossible otherwise.

      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

Exact copied C++ implementation from solution.cpp.

C++ competitive_programming/icpc/2023/H-jet-lag/solution.cpp

Exact copied implementation source.

Raw file
#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

Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.

TeX write-up competitive_programming/icpc/2023/H-jet-lag/solution.tex

Exact copied write-up source.

Raw file
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}

\title{ICPC World Finals 2023\\H. Jet Lag}
\author{}
\date{}

\begin{document}
\maketitle

\section*{Problem Summary}

We must choose sleep intervals so that no activity is missed. Sleeping for $k$ minutes gives $k$ more
minutes during which we are \emph{rested} and cannot fall asleep again, followed by another $k$ minutes
during which we may stay awake or fall asleep. We must start sleeping immediately at time $0$.

\section*{Key Observations}

\begin{itemize}[leftmargin=*]
    \item 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.
    \item 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.
    \]
    \item 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.
    \item 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.
\end{itemize}

\section*{Algorithm}

\begin{enumerate}[leftmargin=*]
    \item If the first activity starts at time $0$, output \texttt{impossible}, because we are required to sleep
    immediately.
    \item Let the free gaps be $[l_i,r_i]=[e_{i-1},b_i]$.
    \item Scan them from left to right, maintaining the farthest reachable time \texttt{reach} in the relaxed
    model:
    \begin{itemize}[leftmargin=*]
        \item if $l_i > \texttt{reach}$, the gap is unreachable;
        \item otherwise sleeping through it would extend reach to $3r_i-2l_i$;
        \item if this extension is larger than the current \texttt{reach}, keep the gap and update
        \texttt{reach}.
    \end{itemize}
    \item If the final \texttt{reach} is smaller than the end of the last activity, output \texttt{impossible}.
    \item 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.
    \item Output the shortened intervals.
\end{enumerate}

\section*{Correctness Proof}

We prove that the algorithm returns the correct answer.

\paragraph{Lemma 1.}
After processing the first $i$ gaps, the variable \texttt{reach} equals the farthest time reachable in the
relaxed model using only those gaps.

\paragraph{Proof.}
We use induction on $i$. Initially \texttt{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. \qed

\paragraph{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.

\paragraph{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. \qed

\paragraph{Theorem.}
The algorithm outputs a valid schedule whenever one exists, and outputs \texttt{impossible} otherwise.

\paragraph{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. \qed

\section*{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)$.

\section*{Implementation Notes}

\begin{itemize}[leftmargin=*]
    \item All times fit in 64-bit signed integers.
    \item The implementation verifies after shortening that each interval still has positive length and still
    reaches the next chosen start.
\end{itemize}

\end{document}