游客 Signup | Login
中文 | En

1967 - 保险丝

现有n个设备,标号分别从1到n,第i个设备的用电量为c[i]。我们有一保险丝,当当前设备使用电量之和超过保险丝的总容量时则会跳闸。接下来有m次操作,初始化所有设备都为关闭状态,每操作一次则改变状态,若当前状态为开启,那么操作完毕后则变为关闭,反之亦然。

Input

测试数据有多组。

输入第一行有3个整数,nmcn(n<=20)表示设备的个数,c表示保险丝的总容量。

接下来有n行,每行一个数字,第i行表示第i个设备的容量。    接下来有m行,每行一个数字k,表示掰动第k个设备的开关。

当n、m、c都为0时,输入结束。

Output

对于每个样例,先输出样例号(见样例),若跳闸了则输出“Fuse was blown.”(引号不输出)。否则先输出“Fuse was not blown.”,然后另起一行输出在进行设备开关的过程中最大的用电量。

具体格式参见样例。

每个样例的最后输出一个空行。

Examples

Input

2 2 10
5
7
1
2
3 6 10
2
5
7
2
1
2
3
1
3
0 0 0

Output

Sequence 1
Fuse was blown.

Sequence 2
Fuse was not blown.
Maximal power consumption was 9 amperes.

Solution C

#include <string.h>
#include <stdio.h>

int a[102];
 
int main()
{
	int n,m,c,max,i,j,f,p,res,t;
	int a[100],o[100];

	t=0;
	while (1==1)
	{
		t++;
		scanf("%d%d%d",&n,&m,&c);

		if (n==0) break;
	
		for (i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}

		max=0;
		memset(o,0,400);
		res=0;

		for (i=1;i<=m;i++)
		{
			scanf("%d",&p);
			if (o[p]==0) o[p]=1; else o[p]=0; 

			if (o[p]==1) 
			{
				res=res+a[p];
				if (res>max) max=res;
			}else res-=a[p];
		}

		printf("Sequence %d\n",t);
	    if (max>c) {printf("Fuse was blown.\n\n"); continue;}

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

	}

	return 0;
}
//0 0 1 1 0 0 0 1
//

Solution C++

#include <cstdio>
#include <vector>
using namespace std;

void solve(int n, int m, int tot) {
    vector < int > c(n + 1), op(m);
    vector < int > flag(n + 1, -1);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &c[i]);
    for (int i = 0; i < m; ++i)
        scanf("%d", &op[i]);
    int sum = 0, ans = 0;
    for (int i = 0; i < m; ++i) {
        int k = op[i];
        flag[k] *= -1;
        sum += flag[k] * c[k];
        ans = max(ans, sum);
        if (sum > tot) {
            puts("Fuse was blown.");
            return;
        }
    }
    puts("Fuse was not blown.");
    printf("Maximal power consumption was %d amperes.\n", ans);
}

int main() {
    int n, m, tot, cas = 0;
    while (scanf("%d %d %d", &n, &m, &tot)) {
        if (0 == n && 0 == m && 0 == tot)
            break;
        printf("Sequence %d\n", ++cas);
        solve(n, m, tot);
        puts("");
    }
    return 0;
}
Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题