(SPOJ) Fila - Solution

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

The solution below keeps the sequence of the queue in an array and the sequence of people who gave up in a hashset. Then, we only need to go through the array checking whether the hashset contains the current value or not.


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();
        int numPeople = Integer.parseInt(line);
       
        int[] people = new int[numPeople];
        line = br.readLine();
        String[] s = line.split(" ");
        for (int i = 0; i < numPeople; i++) {
            people[i] = Integer.parseInt(s[i]);
        }
       
        HashSet<Integer> gone = new HashSet<>();
        line = br.readLine();
        int numGiveUp = Integer.parseInt(line);
        line = br.readLine();
        s = line.split(" ");
        for (int i = 0; i < numGiveUp; i++) {
            gone.add(Integer.parseInt(s[i]));
        }
       
        int count = 0;
        int currNumPeople = numPeople-numGiveUp;
        for (int i = 0; i < numPeople; i++) {
            if (gone.contains(people[i])) {
                continue;
            }
            count++;
            if (count == currNumPeople) {
                bw.write(people[i]+"\n");
                break;
            }
            bw.write(people[i] + " ");
        }
           
        br.close();
        bw.flush();
        bw.close();
        
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução