Star Trek
Time limit1sMemory limit1024 MB
Planets are visited in order, each offering one fuel type; refueling on planet i lets the ship reach the next planet with the same fuel type. Find the minimum number of refuelings to reach planet N and list the planets used.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Binary search, Array
- Solved
- No attempts yet
Problem
An expedition is about to set off on a new-generation spaceship. It will visit planets of a star system in order, from planet Earth to planet Victory. The planets are numbered to in the order they are visited; Earth has number and Victory has number .
To fly between planets, the ship may use any type of fuel that exists in the star system. Before the expedition starts, the ship is on planet Earth and its tank is empty. The existing fuel types are numbered with integers, and on planet the ship can refuel only with fuel of type . When visiting planet , the ship can refuel by emptying the tank of whatever fuel it holds and filling it with fuel of type .
At each planet the refueling station is built so that the tank receives exactly as much fuel as is needed to fly to the next planet with fuel of the same type. If that fuel type never occurs again, refueling at this planet is impossible. In other words, after refueling on planet , the fuel is enough to visit planets through inclusive, where is the smallest planet number such that and . To continue the expedition beyond planet , the ship must refuel again on one of these planets.
Write a program that, given the fuel types on the planets, determines the minimum number of refuelings required for the expedition.
Input
The first line of the input file contains the number (), the number of planets.
The second line of the input file contains integers (), the fuel types on the planets.
Output
In the first line of the output file, print the single number , the minimum number of refuelings that must be performed.
In the second line, print numbers separated by spaces: the numbers of the planets where refueling is required. Print the planet numbers in the order in which the refuelings occur.
If there are several solutions with the minimum number of refuelings, print any of them. If no solution exists, print the number .