(UVA) Through the Desert - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3086


import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String line = br.readLine();
        String[] s = line.split("\\s");
        while (!line.equals("0 Fuel consumption 0")) {
            ArrayList<Event> events = new ArrayList<>();
            
            do {
                int distance = Integer.parseInt(s[0]);
                String event = s[1];
                if (s.length == 4) {
                    int consumption = Integer.parseInt(s[3]);
                    events.add(new Event(distance, event, consumption));
                }
                else {
                    events.add(new Event(distance, event, -1));
                }
                
                line = br.readLine();
                s = line.split("\\s");
            } while (!s[0].equals("0") || !s[1].equals("Fuel"));
            
            int leak = 0;
            double maxFuel = 0;
            double countFuel = 0;
            Event e = events.get(0);
            int oldDistance = e.distance;
            double consumptionByKm = (double)e.consumption/100;
            for (int i = 1; i < events.size(); i++) {
                e = events.get(i);
                int currDistance = e.distance;
                String event = e.event;
                
                countFuel += ((currDistance-oldDistance)*consumptionByKm + (currDistance-oldDistance)*leak);
                
                if (event.equals("Leak")) {
                    leak++;   
                }
                else if (event.equals("Mechanic")) {
                    leak = 0;   
                }
                else if (event.equals("Gas") || event.equals("Goal")) {
                    maxFuel = Math.max(countFuel, maxFuel);
                    countFuel = 0;
                }
                else if (event.equals("Fuel")) {
                    consumptionByKm = (double)e.consumption/100;
                }
                
                oldDistance = currDistance;
            }
            
            System.out.printf("%.3f\n", maxFuel);
            
        }
        
        
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
        
        System.exit(0);
    }
}

class Event {
    int distance;
    String event;
    int consumption;
    
    public Event(int distance, String event, int consumption) {
        this.distance = distance;
        this.event = event;
        this.consumption = consumption;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução