Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedList;
- import java.util.List;
- /*
- * Tuenti Contest
- * Challenge 11 - Gas Stations
- * Author: Pedro Antonio Pardal Jimena
- * Email: pardal@alu.uma.es
- */
- public class GasStations
- {
- private static String formateaLista( List<Integer> gasolineras )
- {
- if ( gasolineras.isEmpty() )
- {
- return "No stops";
- }
- else
- {
- StringBuffer sb = new StringBuffer();
- for ( int gasolinera : gasolineras )
- {
- sb.append( gasolinera + " " );
- }
- return sb.toString().trim();
- }
- }
- private static String calcularParadas( int maxkm, int disttotal, List<Integer> gasolineras )
- {
- List<Integer> visitadas = new LinkedList<Integer>();
- int autonomia = maxkm;
- for ( int i = 0; i < (gasolineras.size() - 1); i++ )
- {
- int actual = gasolineras.get( i );
- int siguiente = gasolineras.get( i + 1 );
- int distancia = siguiente - actual;
- if ( distancia > autonomia )
- {
- visitadas.add( actual );
- autonomia = maxkm - distancia;
- }
- else
- {
- autonomia -= distancia;
- }
- }
- return formateaLista( visitadas );
- }
- public static void main( String[] args )
- {
- BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
- try
- {
- int ncasos = Integer.parseInt( reader.readLine().trim() );
- for ( int i = 0; i < ncasos; i++ )
- {
- int maxkm = Integer.parseInt( reader.readLine().trim() );
- int disttotal = Integer.parseInt( reader.readLine().trim() );
- reader.readLine(); // número de gasolineras
- List<Integer> gasolineras = new LinkedList<Integer>();
- gasolineras.add( 0 );
- for ( String s : reader.readLine().split( " " ) )
- {
- gasolineras.add( Integer.parseInt( s ) );
- }
- gasolineras.add( disttotal );
- String resultado = calcularParadas( maxkm, disttotal, gasolineras );
- System.out.println( resultado );
- }
- }
- catch ( IOException e )
- {
- System.err.println( "Formato de la entrada incorrecto" );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement