(SPOJ) Janela - Solution 1

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

The solution below uses an array to represent the window. We do not need to worry about the height of the window because the glasses cannot move up and down. Then, the parts of the window that are covered by the area of a glass receives '1'. The positions of the array that are not equal to '1' represent the areas that are open.


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

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        int[] window = new int[600];
        String line = br.readLine();
        String[] s = line.split("\\s");
        int l1 = Integer.parseInt(s[0]);
        int l2 = Integer.parseInt(s[1]);
        int l3 = Integer.parseInt(s[2]);

        // the part of the window covered by a glass receives '1'
        for (int i = 0; i < 200; i++) {
            window[l1++] = 1;
            window[l2++] = 1;
            window[l3++] = 1;
        }
       
        int count = 0;
        for (int i = 0; i < 600; i++) {
            if (window[i] == 1) {
                continue;
            }
            count++;
        }
       
        System.out.println(count*100);
       
        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