You are on page 1of 4

Problem 1: Space

Jones has finally achieved his dream and is now exploring the outer space! More specifically, he
is exploring a new and mysterious solar system where several planets revolve on the same orbit
around the system’s star. Jones has traveled from planet to planet and has computed the
amount of fuel needed to go from one planet to the next one.

Jones is curious to discover how many planets he could visit if he would start a journey with
limited fuel, from a particular planet, going either only clockwise or only counter-clockwise on
orbit.

Data Format

Input
The input file is called “space.in”.

The first line contains two numbers, N (the number of planets) and T (the number of queries).

The second line contains N integers: the number at position i (0 <= i < N), d represents the
amount of fuel needed to travel from planet i to planet (i + 1) % N
- The first element is the amount of fuel needed to go from planet 0 to planet 1.
- the last element is the amount of fuel needed to go from the ‘last’ planet to the
‘first’ one, given that they revolve on the same orbit.

Each of the next T lines contains a pair of integers <p, f>:


● p represents the position where you start the journey
● f represent the amount of fuel available

Output
The output file is called “space.out”.
It contains T lines. On each line i there is a single number, ansi representing the answer to the
i’th query.

Data Limits
2 <= N <= 10000
1 <= T <= 1000000
0 <= pi < N
0 <= d, f, ansi <= 1.000.000
For 40% of the tests, T <= 10.

Time limits: C++ (0.6s), Java (3s), Python(5s)

Example

space.in space.out

63 4
161132 3
25 3
53
43

Example explanation

There are 6 planets, numbered from 0 to 5.

From planet 0 to planet 1 (or from 1 to 0) - distance 1


From planet 1 to planet 2 - distance 6
From planet 2 to planet 3 - distance 1
From planet 3 to planet 4 - distance 1
From planet 4 to planet 5 - distance 3
From planet 5 to planet 6 - distance 2
From planet 6 to planet 0 - distance 2

For the first query, you start from planet 2, with 5 units of fuel. If you go ‘clockwise’, you can visit
4 planets (2, 3, 4, 5). If you would choose the other direction, you’d only be able to visit planet 2.

For the second query, you start from planet 5 and from there you visit planet 0 and 1. (distances
2 + 1).

For the third query, you start from planet 4 with 3 units of fuel. In this case the best choice is to
visit planets 4, 3, 2. (‘counter-clockwise’ direction).

Problem 2: Earth
Jones is in trouble (again)! He has discovered a great treasure unattended on one of the more
remote corners of the solar system. He would like to move it back to Earth by traveling on the
direct path from his current position. Unfortunately, he has good reason to doubt his old,
rusty spaceship would be able to make it to the destination. Luckily, he knows that on his path
there are S shipyards, where he can exchange, for a fee, the spaceship he is currently in with a
different one.
With this in mind, Jones would like to know if it possible to reach his destination and if so, what
is the cheapest way to do it.

Data Format

Input
The input file is called “earth.in”.

The first line contains three numbers, R (the maximum range the initial spaceship can travel), D
(the distance to travel) and S (the number of shipyards).

Each of the next S lines contains a tuple of integers <p, c, r>:


● p represents the distance from the starting position to the shipyard
● c represents the cost of exchanging the spaceship
● r represents the maximum range of travel possible with the new spaceship
The list of tuples is sorted by p, ascending.

Output
The output file is called “earth.out”.
It contains a single number representing the minimum cost required to reach Earth, if it can be
reached. Otherwise, it should contain only the number -1.

Data Limits
1 <= S <= 100000
1 <= R <= D
1 <= p <= D <= 1.000.000
1 <= c <= 1.000.000

For 40% of the tests, D <= 1000

Time limits: C++ (0.6s), Java (7s), Python(10s)

Example 1

earth.in earth.out

7 30 5 80
6 50 8
7 20 7
12 40 9
13 50 11
23 10 7

First example explanation - trip possible, display cost


At first, you can only travel a maximum of 7. Thus, you can only reach the shipyards at points 6
and 7. If you look at both their maximum ranges, they can both go as far as point 14. The only
difference between them is that point 7 has a lower cost than point 6, so it would be more
profitable to choose to exchange the ship at point 7.
The points that can be reached now are 12 and 13, We can see that point 12 has a lower cost,
but its range won’t get Jones to the next shipyard (23), as it can only get as far as 12 + 9 = 21.
However, the other available shipyard (13), can reach as far as 13 + 11 = 24 , which makes it
possible to reach the last shipyard, at point 23.
This last shipyard offers a spaceship which can travel 7 more units, thus reaching point 30, the
end of the journey. We display the total cost of the shipyards we used.

Example 2

earth.in earth.out

7 30 5 -1
6 50 8
7 20 7
12 40 9
13 50 11
25 10 7

Second example explanation - trip impossible


At first, you can only travel a maximum of 7. Thus, you can only reach the shipyards at points 6
and 7. If you look at both their maximum ranges, they can both go as far as point 14. The only
difference between them is that point 7 has a lower cost than point 6, so it would be more
profitable to choose to exchange the ship at point 7.
The points that can be reached now are 12 and 13, We can see that point 12 has a lower cost,
but its range won’t get Jones to the next unreachable shipyard (25), as it can only get as far as
12 + 9 = 21. The other available shipyard (13), can reach as far as 13 + 11 = 24 , which is also
before the shipyard at point 25. We conclude that completing the journey is impossible, so we
display only -1, to indicate that.

You might also like