Professional Documents
Culture Documents
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.
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.
Example
space.in space.out
63 4
161132 3
25 3
53
43
Example explanation
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).
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
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
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