2009年2月13日 星期五

Q661: Blowing Fuses

// Run time: 0.000
#include <stdio.h>
#define N 20
int arr[N];
bool state[N];
int main()
{
int n; // how many electric
int m; // time's? open or close
int c; // capacity
int i; // for loop's count
int sum; // total
int tmp; // using for scanf
int max; // maximum
int count = 0; // which sequence?
bool blown; // blown or not

while (scanf("%d%d%d", &n, &m, &c))
{
if (!n && !m && !c) break;

// n台電器的電流,而且初始都是關著
for ( i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
state[i] = false;
}

max = 0;
sum = 0;
blown = false;

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

// 原本是open
if (state[tmp-1])
{
sum -= arr[tmp-1];
state[tmp-1] = false;
}

// 原本是close
else
{
sum += arr[tmp-1];
if (sum > c)
blown = true;

if (max < sum)
max = sum;
state[tmp-1] = true;
}

}

printf("Sequence %d\n", ++count);

if (blown)
printf("Fuse was blown.\n\n");
else
{
printf("Fuse was not blown.\n");
printf("Maximal power consumption was %d amperes.\n\n", max);
}
}

return 0;
}

沒有留言: