【題解】ZeroJudge c094: 00661 – Blowing Fuses

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c094
【解題想法】在開關過程中,只要所有使用中的電器的總用電量,曾經超過保險絲的容量,保險絲就會燒掉。

#include <iostream>
using namespace std;
int a[25], on[25];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, c, Case = 1, x;
    while (cin >> n >> m >> c){
        if (n+m+c == 0) break;
        
        for (int i = 1; i <= n; i++){
            cin >> a[i];
            on[i] = 0;
        }
        int sum = 0, mx = 0;
        bool blown = false;
        while (m--){
            cin >> x;
            if (on[x] == 0){
                on[x] = 1;
                sum += a[x];
                mx = max(mx, sum);
                if (sum > c){
                    blown = true;
                }
            } else {
                on[x] = 0;
                sum -= a[x];
            }
        }
        if (Case > 1) cout << "\n";
        cout << "Sequence " << Case++ << "\n";
        if (blown) cout << "Fuse was blown.\n";
        else {
            cout << "Fuse was not blown.\n";
            cout << "Maximal power consumption was " << mx << " amperes.\n";
        }
        //cout << "\n";
    }
    return 0;
}
分享本文 Share with friends