ZeroJudge C094: Blowing Fuses

UVa 00661 – Blowing Fuses

This is a task for which you can create a program that checks whether the total power consumption of all appliances in use will exceed the capacity of the fuse after turning on certain appliance switches. You can accomplish this by keeping track of the power consumption of each appliance and updating the total power consumption whenever an appliance is turned on or off. Then, you can compare the total power consumption with the capacity of the fuse to determine if it will be exceeded.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
EOF inputs: each set of test data begins with a line containing three integers: N, M, and C. Here, N represents the total number of electrical appliances (N ≤ 20), M represents the total number of times electrical appliances are switched on or off, and C represents the capacity of the fuse.
Following the first line, there will be N lines, each containing an integer i, representing the current required by the respective electrical appliance when in use, where i corresponds to the 1st, 2nd, ..., Nth electrical appliance, respectively.
Following that, there will be M lines, each containing an integer K ranging from 1 to N, representing the action of toggling the state (on/off) of the Kth electrical appliance. Note: For each electrical appliance, the first action is to turn it on, and the second action is to turn it off, and so on. Assume that initially, all electrical appliances are turned off.
N = M = C = 0 indicates the end of the input.
For each test case, if the fuse blows, please output: "Fuse was blown.". If the fuse does not blow, output: "Fuse was not blown." and output the maximum total power consumption during the switching process. After each set of test data, please output one blank line. Please refer to the Sample Output.
2 2 10
5
7
1
2
3 6 10
2
5
7
2
1
2
3
1
3
0 0 0
Sequence 1
Fuse was blown.

Sequence 2
Fuse was not blown.
Maximal power consumption was 9 amperes.
ZeroJudge C094 範例測資

Thought Process

Use a Map to record whether each appliance is on or off. When declaring Map, all values are initialized to 0 by default.

You can declare a count variable to keep track of the current sequence number. Instead of immediately outputting a newline, output it first if the count is not 1. So, if count != 1, output a newline first, then output the sequence number and whether the fuse was blown.

Sample Code-ZeroJudge C094: Blowing Fuses

#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N, M, C, count = 1;
    while (cin >> N >> M >> C && N != 0 && M != 0 && C != 0)
    {
        vector<int>num;
        num.push_back(-1);
        int power = 0, max = -9999;
        map<int, int>MAP;
        for (int i = 0; i<N; i++)
        {
            int tmp;
            cin >> tmp;
            num.push_back(tmp);
        }
        bool blown = false;
        for (int i = 0; i<M; i++)
        {
            int tmp;
            cin >> tmp;
            if (MAP[tmp] == 0)
            {
                MAP[tmp]++;
                power += num[tmp];
            }
            else
            {
                MAP[tmp]--;
                power -= num[tmp];
            }
            if (power > C) blown = true;
            if (power > max) max = power;
        }
        if (count != 1) cout << "\n";
        cout << "Sequence " << count << "\n";
        if (blown) cout << "Fuse was blown.\n";
        else cout << "Fuse was not blown.\nMaximal power consumption was " << max << " amperes.\n";
        count++;
    }
}

//ZeroJudge C094
//Dr. SeanXD

Comments