(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<>();
int[] tempoFuncionarios = new int[numFuncionarios];
int[] numItensClientes = new int[numClientes];
for (int i = 0; i < numFuncionarios; i++) {
tempoFuncionarios[i] = sc.nextInt();
}
for (int i = 0; i < numFuncionarios; i++) {
queue.add(new Funcionario(i, 0));
}
for (int i = 0; i < numClientes; i++) {
numItensClientes[i] = sc.nextInt();
}
for (int i = 0; i < numClientes; i++) {
Funcionario f = queue.poll();
int currId = f.id;
int currTempo = f.tempo;
int tempoDessaCompra = numItensClientes[i]*tempoFuncionarios[currId];
queue.add(new Funcionario(currId, currTempo+tempoDessaCompra));
}
int maxTempo = -1;
while (queue.size() > 0) {
Funcionario f = queue.poll();
maxTempo = Math.max(f.tempo, maxTempo);
}
System.out.println(maxTempo);
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Funcionario implements Comparable<Funcionario> {
int id;
int tempo;
public Funcionario(int id, int tempo) {
this.id = id;
this.tempo = tempo;
}
public int compareTo(Funcionario f) {
if (this.tempo-f.tempo == 0) {
return this.id-f.id;
}
return this.tempo-f.tempo;
}
}
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<>();
int[] tempoFuncionarios = new int[numFuncionarios];
int[] numItensClientes = new int[numClientes];
for (int i = 0; i < numFuncionarios; i++) {
tempoFuncionarios[i] = sc.nextInt();
}
for (int i = 0; i < numFuncionarios; i++) {
queue.add(new Funcionario(i, 0));
}
for (int i = 0; i < numClientes; i++) {
numItensClientes[i] = sc.nextInt();
}
for (int i = 0; i < numClientes; i++) {
Funcionario f = queue.poll();
int currId = f.id;
int currTempo = f.tempo;
int tempoDessaCompra = numItensClientes[i]*tempoFuncionarios[currId];
queue.add(new Funcionario(currId, currTempo+tempoDessaCompra));
}
int maxTempo = -1;
while (queue.size() > 0) {
Funcionario f = queue.poll();
maxTempo = Math.max(f.tempo, maxTempo);
}
System.out.println(maxTempo);
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Funcionario implements Comparable<Funcionario> {
int id;
int tempo;
public Funcionario(int id, int tempo) {
this.id = id;
this.tempo = tempo;
}
public int compareTo(Funcionario f) {
if (this.tempo-f.tempo == 0) {
return this.id-f.id;
}
return this.tempo-f.tempo;
}
}
Comments
Post a Comment