(SPOJ) Janela - Solution 2

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


If you want to see another solution for this problem, click here.

Instead of using an array to represent the window as we did in the previous solution, we are going to pass through all the positions of the glasses and check where they start and where they end. All the area that is covered by a glass is kept in order to get the total area that is not covered.


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

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
        Window[] windows = new Window[6];
        int index = 0;
     
        String line = br.readLine();
        String[] s = line.split("\\s");
     
        int l1 = Integer.parseInt(s[0]);
        windows[index++] = new Window(1, l1, true);
        windows[index++] = new Window(1, l1+200, false);
        int l2 = Integer.parseInt(s[1]);
        windows[index++] = new Window(2, l2, true);
        windows[index++] = new Window(2, l2+200, false);
        int l3 = Integer.parseInt(s[2]);
        windows[index++] = new Window(3, l3, true);
        windows[index++] = new Window(3, l3+200, false);

        Arrays.sort(windows);
      
        int sum = 0;
        int id = -1;
        int start = -1;
        int end = -1;
        for (int i = 0; i < 6; i++) {
            Window curr = windows[i];
            if (start == -1) {
                id = curr.id;
                start = curr.coord;
                end = curr.coord+200;
            }
          
            if (curr.isStart) {
                id = curr.id;
                end = curr.coord+200;
            }
            else if (curr.coord == end && id == curr.id) {
                sum += (end-start);
                start = -1;
            }
        }
      
        System.out.println((600-sum)*100);
     
        return;
    }
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
     
        System.exit(0);
    }
}

class Window implements Comparable<Window> {
    int id;
    int coord;
    boolean isStart;
  
    public Window(int i, int c, boolean iS) {
        id = i;
        coord = c;
        isStart = iS;
    }

    public int compareTo(Window w) {
        if (this.coord-w.coord == 0) {
            return this.id-w.id;
        }
        return this.coord-w.coord;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução