15/08/2013

661 - Blowing Fuses - Java

import java.io.*;  
 import java.util.*;  
 public class BlowingFuses {  
      public static void main(String[] args) throws Exception {  
 //          FileWriter writer = new FileWriter("output.out");  
 //          BufferedWriter out = new BufferedWriter(writer);  
           Reader.init(System.in);  
 //          Reader.init(new FileInputStream(new File("input.in")));  
           int currentCase = 1;  
           int noOfDevices = Reader.nextInt();  
           int noOfOperations;  
           int capacity;  
           int currentConsumption;  
           int maxConsumption;  
           boolean[] devicesState;  
           int[] devicesConsumption;  
           int tempDevice;  
           boolean isDone;  
           while (noOfDevices != 0) {  
                System.out.println("Sequence " + currentCase);  
                currentConsumption = 0;  
                maxConsumption = 0;  
                noOfOperations = Reader.nextInt();  
                capacity = Reader.nextInt();  
                devicesState = new boolean[noOfDevices];  
                devicesConsumption = new int[noOfDevices];  
                isDone = false;  
                for(int i = 0; i < noOfDevices; i++) {  
                     devicesConsumption[i] = Reader.nextInt();  
                     devicesState[i] = false;  
                }  
                for(int i = 0; i < noOfOperations; i++) {  
                     tempDevice = Reader.nextInt() - 1;  
                     if(!devicesState[tempDevice]) {  
                          //power on...  
                          devicesState[tempDevice] = true;  
                          currentConsumption += devicesConsumption[tempDevice];  
                          if(currentConsumption > capacity) {  
                               for(i = i + 1; i < noOfOperations; i++) Reader.nextInt();  
                               System.out.println("Fuse was blown.\n");  
                               isDone = true;  
                          }  
                          if(currentConsumption > maxConsumption) maxConsumption = currentConsumption;  
                     } else {  
                          //shutting down...  
                          currentConsumption -= devicesConsumption[tempDevice];  
                          devicesState[tempDevice] = false;  
                     }  
                }  
                if(!isDone) {  
                     System.out.println("Fuse was not blown.");  
                     System.out.println("Maximal power consumption was " + maxConsumption + " amperes.\n");  
                }  
                noOfDevices = Reader.nextInt();  
                currentCase++;  
           }  
 //          out.close();  
      }  
 }  
 /** Class for buffered reading int and double values */  
 /** http://www.cpe.ku.ac.th/~jim/java-io.html */  
 class Reader {  
      static BufferedReader reader;  
      static StringTokenizer tokenizer;  
      /** call this method to initialize reader for InputStream */  
      static void init(InputStream input) {  
           reader = new BufferedReader(new InputStreamReader(input));  
           tokenizer = new StringTokenizer("");  
      }  
      /** get next word */  
      static String next() throws IOException {  
           while (!tokenizer.hasMoreTokens()) {  
                tokenizer = new StringTokenizer(reader.readLine());  
           }  
           return tokenizer.nextToken();  
      }  
      static int nextInt() throws IOException {  
           return Integer.parseInt(next());  
      }  
      static double nextDouble() throws IOException {  
           return Double.parseDouble(next());  
      }  
 }  

1 comment:

  1. A guy is standing on the corner of the street smoking one cigarette after another. A lady walking by notices him and says
    "Hey, don't you know that those things can kill you? I mean, didn't you see the giant warning on the box?!"
    "That's OK" says the guy, puffing casually "I'm a computer programmer"
    "So? What's that got to do with anything?"
    "We don't care about warnings. We only care about errors."

    ReplyDelete