1967 - 保险丝
时间限制 : 1 秒
内存限制 : 32 MB
现有n个设备,标号分别从1到n,第i个设备的用电量为c[i]。我们有一保险丝,当当前设备使用电量之和超过保险丝的总容量时则会跳闸。接下来有m次操作,初始化所有设备都为关闭状态,每操作一次则改变状态,若当前状态为开启,那么操作完毕后则变为关闭,反之亦然。
题目输入
测试数据有多组。
输入第一行有3个整数,n、m、c,n(n<=20)表示设备的个数,c表示保险丝的总容量。
接下来有n行,每行一个数字,第i行表示第i个设备的容量。 接下来有m行,每行一个数字k,表示掰动第k个设备的开关。
当n、m、c都为0时,输入结束。
题目输出
对于每个样例,先输出样例号(见样例),若跳闸了则输出“Fuse was blown.”(引号不输出)。否则先输出“Fuse was not blown.”,然后另起一行输出在进行设备开关的过程中最大的用电量。
具体格式参见样例。
每个样例的最后输出一个空行。
输入/输出样例
输入格式
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.
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 //
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; }
Java解答
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sequence = 1; while (true) { int n = scanner.nextInt(); int m = scanner.nextInt(); int c = scanner.nextInt(); if (n == 0 && m == 0 && c == 0) { break; } int[] array = new int[n + 1]; for (int i = 1; i <= n; i++) { array[i] = scanner.nextInt(); } boolean[] array2 = new boolean[n + 1]; int count = 0; boolean flag = false; int max = 0; for (int i = 0; i < m; i++) { int index = scanner.nextInt(); if (array2[index]) { array2[index] = false; count -= array[index]; } else { array2[index] = true; count += array[index]; max = Math.max(max, count); if (count > c) { flag = true; } } } System.out.println("Sequence " + sequence); if (flag) { System.out.println("Fuse was blown."); } else { System.out.println("Fuse was not blown."); System.out.println("Maximal power consumption was " + max + " amperes."); } System.out.println(); sequence++; } scanner.close(); } }