(SPOJ) Banco - Solution

Link to the problem: http://br.spoj.com/problems/BANCO12/

The solution below keeps a queue with the employees in order to discover who is the next employee that will serve the client.For every attendance, we check if the time that the employee will start to talk to the client minus the time that this client arrived at the bank is bigger than the 20 minutes.


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

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
      
        String line = br.readLine();
        String[] s = line.split(" ");
        int numEmployees = Integer.parseInt(s[0]);
        int numPeople = Integer.parseInt(s[1]);
       
        People[] people = new People[numPeople];
        for (int i = 0; i < numPeople; i++) {
            line = br.readLine();
            s = line.split(" ");
            int entryTime = Integer.parseInt(s[0]);
            int duration = Integer.parseInt(s[1]);
            people[i] = new People(entryTime, duration);
        }
       
        Queue<Employee> queue = new PriorityQueue<>();
        Employee[] employees = new Employee[numEmployees];
        for (int i = 0; i < numEmployees; i++) {
            employees[i] = new Employee(0);
            queue.add(employees[i]);
        }
       
        int waitTooMuch = 0;
        for (int i = 0; i < numPeople; i++) {
            Employee e = queue.poll();
           
            int startAttending = Math.max(e.attendance, people[i].entryTime);
            int timeWaiting = startAttending-people[i].entryTime;
            if (timeWaiting > 20) {
                waitTooMuch++;
            }
            e.attendance = startAttending+people[i].duration;
            queue.add(e);
        }
               
        bw.write(waitTooMuch+"\n");
       
        bw.flush();
        bw.close();
      
        return;
    }
  
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
      
        System.exit(0);
    }
}

class People {
    int entryTime;
    int duration;
   
    public People(int entryTime, int duration) {
        this.entryTime = entryTime;
        this.duration = duration;
    }
}

class Employee implements Comparable<Employee> {
    int attendance;
   
    public Employee(int a) {
        attendance = a;
    }
   
    public int compareTo(Employee e) {
        return this.attendance-e.attendance;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução