UVa 661 - Blowing Fuses

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

直接模擬即可。

Code

UVa 661
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<cstdio>

int main()
{
bool state[21];
int cost[21];
int n, m, c, Case = 1;
while (scanf("%d%d%d", &n, &m, &c) && (n || m || c))
{
int i, k, now = 0, max = 0;
for (i = 1; i <= n; i++)
state[i] = false;
for (i = 1; i <= n; i++)
scanf("%d", &cost[i]);

for (i = 0; i < m; i++)
{
scanf("%d", &k);

if (max != -1)
{
state[k] = !state[k];
if (state[k])
now += cost[k];
else
now -= cost[k];

if (now <= c)
{
if (now > max)
max = now;
}
else
max = -1;
}
}

printf("Sequence %d\n", Case++);
if (max == -1)
puts("Fuse was blown.\n");
else
{
puts("Fuse was not blown.");
printf("Maximal power consumption was %d amperes.\n\n", max);
}
}

return 0;
}