Posts

(SPOJ) Caça ao tesouro - Solution

Link to the problem: http://br.spoj.com/problems/TESOUR11/ For each clue that we have, we need to know all its possible ends. Moreover, in each possible end we keep a counter associated with how many clues move us to that position. Then, we know how to determine for sure where the treasure is if there is only one position where the counter is equal to the number of clues. import java.io.*; import java.util.*; class Main {     public int[] vi = {-1,0,0,1};     public int[] vj = {0,-1,1,0};         public void process() throws NumberFormatException, IOException {         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 String line = br.readLine();         String[] s = line.split("\\s");         int size = Integer...

(SPOJ) Marciano - Solution

Link to the problem: http://br.spoj.com/problems/MARCIAN1/ For this problem, we need to use some mathematical concepts. 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");         int l = Integer.parseInt(s[0]);         int a = Integer.parseInt(s[1]);         int p = Integer.parseInt(s[2]);         int r = Integer.parseInt(s[3]);                 int maiorLado = Math.max(l, a);   ...

(URI) Fila do Supermercado - Solution

Link to the problem: https://www.urionlinejudge.com.br/judge/pt/problems/view/2065 The solution below keeps a queue with the employees in order to discover who is the next employee that will process a purchase. This queue is sorted according to the time that the employee needs to process a whole purchase that was assigned to him/her. If two or more employees have the same time, the employee is chosen through his/her id. import java.io.*; import java.util.*; class Main {     public void process() throws NumberFormatException, IOException {         Scanner sc = new Scanner(System.in);                 int numFuncionarios = sc.nextInt();         int numClientes = sc.nextInt();                 Queue<Funcionario> queue = new PriorityQueue<>();  ...

(URI) Jogo do Quadrado - Solution

Link to the problem: https://www.urionlinejudge.com.br/judge/pt/problems/view/2067 import java.io.*; import java.util.*; class Main {     public void process() throws NumberFormatException, IOException {         Scanner sc = new Scanner(System.in);                 int numLinhas = sc.nextInt();         int numColunas = sc.nextInt();         int[][] matriz = new int[numLinhas][numColunas];         for (int i = 0; i < numLinhas; i++) {             for (int j = 0; j < numColunas; j++) {                 int n = sc.nextInt();                 if (n == 0) {  ...

(SPOJ) Matriz Escada - Solution

Link to the problem: http://br.spoj.com/problems/ESCADA14/ import java.io.*; import java.util.*; class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int rows = sc.nextInt();         int cols = sc.nextInt();                 int[][] matrix = new int[rows][cols];         for (int i = 0; i < rows; i++) {             for (int j = 0; j < cols; j++) {                 matrix[i][j] = sc.nextInt();             }         }             ...

(SPOJ) Fechadura - Solution

Link to the problem: http://br.spoj.com/problems/FECHAD14/ The solution below used Greedy algorithm to solve this problem. Starting from the left, we increase or decrease the height of pin n and pin n+1 . We only move forward when pin n has the desired height. 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");         int numPins = Integer.parseInt(s[0]);         int height = Integer.parseInt(s[1]);                 line = br.readLine();  ...

(SPOJ) Famílias de Troia - Solution

Link to the problem: http://br.spoj.com/problems/TROIA13/ For this problem, we need to determine the amount of distinct families. First, we can map the entries into an adjacency list. Then, we call the Breadth-First Search (BFS) method, which will visit all the members of a specific family. Finally, we only need to count how many times the BFS method was called, and we will know how many distinct families there are. import java.io.*; import java.util.*; class Main {     public HashMap<Integer, ArrayList<Integer>> adjList;     public HashSet<Integer> visited;         public void bfs(int curr) {         Queue<Integer> queue = new ArrayDeque<Integer>();         queue.add(curr);                 while (queue.size() > 0) {       ...