#UVa:661-Blowing Fuses

灆洢 2016-04-28 01:42:38

照著題目要求去紀錄每次開關所用的電流量以及裝置開關狀態即可得解。

C++(0.000)

/*******************************************************/
/* UVa 661 Blowing Fuses                               */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2016/04/28                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

int main(){
  int n, m, c;
  int caseNumber = 0;
  while( scanf("%d%d%d", &n, &m, &c) != EOF &&
         ( n != 0 || m != 0 || c != 0 ) ){
    int consumption[25] = {0};
    for( int i = 1 ; i <= n ; ++i ){
      scanf("%d", &consumption[i]);
    }         

    bool isTurnOn[25] = {false};
    int nowUsed = 0, maxUsed = 0;
    int operation;
    for( int i = 0 ; i < m ; ++i ){
      scanf("%d", &operation);

      if( isTurnOn[operation] ){
        nowUsed -= consumption[operation];
      }
      else{
        nowUsed += consumption[operation];
        maxUsed = max(maxUsed, nowUsed);
      }

      isTurnOn[operation] = !isTurnOn[operation];
    }

    printf("Sequence %d\n", ++caseNumber);
    if( maxUsed > c ){
      printf("Fuse was blown.\n");
    }
    else{
      printf("Fuse was not blown.\n");
      printf("Maximal power consumption was %d amperes.\n", maxUsed);
    }
    printf("\n");
  }
  return 0;
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料