(UVA) Bankrupt Baker - Solution

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

A map structure is used to keep the information about the ingredients. For every ingredient in a recipe, this map is used to discover how much it would cost to use the required amount of ingredients.


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

class Main {
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
        
        int numBinders = sc.nextInt();
        for (int i = 0; i < numBinders; i++) {
            sc.nextLine();
            String name = sc.nextLine();
            int numIngredients = sc.nextInt();
            int numRecipes = sc.nextInt();
            int budget = sc.nextInt();
            
            // read ingredients
            HashMap<String, Integer> ingredients = new HashMap<>();
            for (int j = 0; j < numIngredients; j++) {
                String ingredient = sc.next();
                int cost = sc.nextInt();
                ingredients.put(ingredient, cost);                
            }

            // read recipes            
            Recipe[] output = new Recipe[numRecipes];
            for (int j = 0; j < numRecipes; j++) {
                sc.nextLine();
                String nameRecipe = sc.nextLine();
                int numIngredientsRecipe = sc.nextInt();
                int totalCost = 0;
                // read ingredients and quantity
                for (int k = 0; k < numIngredientsRecipe; k++) {
                    String ingredient = sc.next();
                    int qty = sc.nextInt();
                    if (!ingredients.containsKey(ingredient)) {
                        continue;
                    }
                    totalCost += (ingredients.get(ingredient)*qty);
                }
                output[j] = new Recipe(nameRecipe, totalCost);
            }
            
            int count = 0;
            Arrays.sort(output);
            System.out.println(name.toUpperCase());
            for (int j = 0; j < numRecipes; j++) {
                if (output[j].cost <= budget) {
                    System.out.println(output[j].name);
                    count++;
                }
            }
            
            if (count == 0) {
                System.out.println("Too expensive!");
            }
            
            System.out.println();
        }
    }
    
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
        
        System.exit(0);
    }
}

class Recipe implements Comparable<Recipe> {
    String name;
    int cost;
    
    public Recipe(String n, int c) {
        name = n;
        cost = c;
    }
    
    public int compareTo(Recipe r) {
        if (this.cost-r.cost == 0) {
            return this.name.compareTo(r.name);        
        }
        
        return this.cost-r.cost;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução