Official Analysis (C++)

Explanation

Observation

We can think of the output slots as positions on the number line. Consider a wagon that starts moving at time 00. Then, that wagon can catch a candy cic_i (si,ti)(s_i, t_i) if its position at time 00 is within the range [siti,si+ti][s_i - t_i, s_i + t_i]. Let's define this range as the range of acceptable wagon positions for cic_i at time 00. With every passing second, this range is reduced by 11 unit to the left and to the right of sis_i. Generally, the range of acceptable wagon positions for cic_i at time tt (where ttit \leq t_i) is:

[si(tit),si+(tit)][s_i - (t_i - t), s_i + (t_i - t)]

At time tit_i this range has been shrunk to a single point, sis_i.

A wagon can catch a candy cjc_j (sj,tj)(s_j, t_j) before cic_i, if the range of acceptable wagon positions for ii at time tjt_j includes sjs_j, (since the wagon will be at sjs_j to catch jj). However, at time tjt_j the range for acceptable wagon positions of cic_i will have been reduced to

[si(titj),si+(titj)][s_i - (t_i - t_j), s_i + (t_i - t_j)]

But sjs_j needs to be inside this range, therefore:

si(titj)sj and sjsi+(titj)    s_i - (t_i - t_j) \leq s_j \text{ and } s_j \leq s_i + (t_i - t_j) \iff
sitisjtj and sj+tjsi+tis_i - t_i \leq s_j - t_j \text{ and } s_j + t_j \leq s_i + t_i

This is equivalent to saying that the range of acceptable wagon positions of cjc_j is fully contained within the range of cic_i.

We have therefore concluded that two candies can be caught by the same wagon if (and only if) their ranges of acceptable wagon positions are nested.

Finding the minimum number of wagons

We can think of each candy as its range of acceptable wagon positions at time 0 [st,s+t][s - t, s + t] (note that each candy corresponds to a unique range and vice versa). We can conclude that a wagon can catch a sequence of candies c1,c2,...,ckc_1, c_2, ..., c_k, if they form a sequence of nested ranges. It follows that the minimum number of wagons needed is the longest sequence of non-nested ranges.

Two ranges [l1,r1][l_1, r_1], [l2,r2][l_2, r_2] that are not nested adhere to: l1<l2    r1<r2l_1 < l_2 \implies r_1 < r_2.

If we sort the candies by the left endpoint of their ranges such that

l1l2...lnl_1 \leq l_2 \leq ... \leq l_n

then a sequence of non-nested ranges would correspond to an increasing sequence of right endpoints. Therefore, the minimum number of required wagons would correspond to the length of the longest increasing sequence of right endpoints.

Assigning the candies to wagons

By following the binary search algorithm described in the LIS module, we can assign the candies with the same LIS length to the same wagon. The ranges that have the same LIS length cannot be nested. If they were, we could use the inside range to increase the LIS of the outside one by 1.

Implementation

Time Complexity: O(NlogN)\mathcal{O}(N \log N)

Warning!

Watch out for the case li=ljl_i = l_j, in which case we must sort by decreasing right endpoint to ensure they would not be counted as non-nested ranges.

C++

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
int main() {
int n;
cin >> n;
vector<pi> candies(n);

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!