1484 Blowing Fuses

1484 Blowing Fuses

解答例

import java.util.*;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int cnt = 0;
        while(true){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int c = sc.nextInt();
            
            if(n==0 && m==0 && c==0) break;
            
            cnt++;
            int dev_consume[] = new int[n];
            boolean dev_on[] = new boolean[n];
            int toggle[] = new int[m];
            
            for(int i=0;i<n;i++){
                dev_consume[i] = sc.nextInt();
            }
            Arrays.fill(dev_on, false);
            for(int i=0;i<m;i++){
                toggle[i] = sc.nextInt() - 1;
            }
            
            int total = 0;
            int max = 0;
            boolean blowup = false;
            for(int i=0;i<m;i++){
                int k = toggle[i];
                if(dev_on[k]){
                    dev_on[k] = false;
                    total -= dev_consume[k];
                }
                else{
                    dev_on[k] = true;
                    total += dev_consume[k];
                }
                
                if(total>c){
                    blowup = true;
                    break;
                }
                else max = Math.max(max, total);
            }
            
            System.out.printf("Sequence %d\r\n", cnt);
            if(blowup){
                System.out.println("Fuse was blown.");
            }
            else{
                System.out.println("Fuse was not blown.");
                if(max<=1) System.out.printf("Maximal power consumption was %d amperes.\r\n", max);
                else System.out.printf("Maximal power consumption was %d amperes.\r\n", max);
            }
            System.out.println();
        }
    }
}

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2006年05月14日 00:18
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。