## IOI '11 P5 - Dancing Elephants

View as PDF

Points: 40 (partial)
Time limit: 4.0s
Memory limit: 256M

Problem type
Allowed languages
C, C++

Dancing Elephants is a spectacular show in Pattaya that features elephants dancing on a line, known as the stage.

After years of training, elephants in this show are capable of many amazing dances. The show consists of a series of acts. In each act, exactly one elephant performs a cute dance while possibly moving to a different position.

The show producers want to produce a photo book that contains pictures of the entire show. After each act, they want to take pictures of all elephants as seen by the spectators.

At any time during the show, multiple elephants may share the same position. In that case, they simply stand behind one another at the same position.

A single camera can take a picture of a group of elephants if and only if all their positions lie on some segment of length (including both its endpoints). As the elephants can spread out across the stage, multiple cameras may be needed in order to take simultaneous snapshots of all the elephants.

As an example, suppose that and that the elephants are at positions , , , and on the stage. At this moment, a single camera can take their picture, as shown below. (Elephants are shown as triangles; cameras are shown as trapezoids.)

In the following act, the elephant at position dances to position . After this act, we need at least two cameras to take the snapshot.

In the next act, the elephant at position moves to position . For the new arrangement of elephants, we need three cameras to photograph all of them.

In this interactive task, you have to determine the minimum number of cameras needed to take the pictures after each of the acts. Note that the number of cameras needed may increase, decrease, or stay the same between acts.

Write the following procedures:

• Procedure init(N,L,X) that takes the following parameters:

• – the number of elephants. The elephants are numbered through .
• – the length of the segment captured by a single camera. You may assume that is an integer such that .
• – a one-dimensional array of integers representing the initial positions of the elephants. For , elephant starts at the position . The initial positions are in sorted order. More precisely, you may assume that . Note that during the dance the elephants may reorder themselves.

This procedure will be called only once, prior to all calls to update. It does not return any value.

• Procedure update(i,y) that takes the following parameters:

• – the number of the elephant that moves in the current act.
• – the position where the elephant will stand after the current act. You may assume that is an integer such that .

This procedure will be called multiple times. Each call corresponds to a single act (which follows on from all of the previous acts). Each call must return the minimum number of cameras needed to photograph all elephants after the corresponding act.

#### Example

Consider the case where , , and the initial positions of the elephants are .

First, your procedure init will be called with these parameters. Afterwards, your procedure update will be called once for each act. Here is an example sequence of calls and their correct return values:

act call parameters return value
1 update(2,16) 1
2 update(1,25) 2
3 update(3,35) 2
4 update(0,38) 2
5 update(2,0) 3

• There are exactly elephants.
• Initially, and after each act, the positions of all elephants will be distinct.
• Your procedure update will be called at most times.
• .
• Initially, and after each act, the positions of all elephants will be distinct.
• Your procedure update will be called at most times.
• .
• Initially, and after each act, the positions of all elephants will be distinct.
• Your procedure update will be called at most times.
• .
• Elephants may share the same position.
• Your procedure update will be called at most times.
• .
• Elephants may share the same position.
• Your procedure update will be called at most times.
• Note: The collection templates in the C++ Standard Library (STL) can be slow; in particular, it might not be possible to solve subtask 5 if you use them.

#### Implementation details

##### Interface (API)

To be implemented by contestant:

void init(int N, int L, int[] X);
int update(int i, int y);