Blowing Fuses

FJNU.1685

Description
Maybe you are familiar with the following situation. You have plugged in a lot of electrical devices, such as toasters, refrigerators, microwave ovens, computers, stereos, etc, and have them all running. But at the moment when you turn on the TV, the fuse blows, since the power drawn from all the machines is greater than the capacity of the fuse. Of course this is a great safety feature, avoiding that houses burn down too often due to fires ignited by overheating wires. But it is also annoying to walk down to the basement (or some other inconvenient place) to replace to fuse or switch it back on.
What one would like to have is a program that checks before turning on an electrical device whether the combined power drawn by all running devices exceeds the fuses capacity (and it blows), or whether it is safe to turn it on.

Input
The input consists of several test cases. Each test case describes a set of electrical devices and gives a sequence of turn on/off operations for these devices.
The first line of each test case contains three integers n, m and c, where n is the number of devices (n <= 20), m the number of operations performed on these devices and c is the capacity of the fuse (in Amperes). The following n lines contain one positive integer ci each, the consumption (in Amperes) of the i-th device.
This is followed by m lines also containing one integer each, between 1 and n inclusive. They describe a sequence of turn on/turn off operations performed on the devices. For every number, the state of that particular devices is toggled, i.e. if it is currently running, it is turned off, and if it is currently turned off, it will by switched on. At the beginning all devices are turned off.
The input will be terminated by a test case starting with n = m = c = 0. This test case should not be processed.

Output
For each test case, first output the number of the test case. Then output whether the fuse was blown during the operation sequence. The fuse will be blown if the sum of the power consumptions ci of turned on devices at some point exceeds the capacity of the fuse c.
If the fuse is not blown, output the maximal power consumption by turned on devices that occurred during the sequence.
Output a blank line after each test case.

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

Sample Output
Sequence 1
Fuse was blown.

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

Source
Southwestern Europe 1998

My Program

#include<iostream>
#define N 21
using namespace std;

int main()
{
    
bool use[N],flag;
    
int cost[N];
    
int k=1,n,m,c,i,x,max,now;
    
while(cin>>n>>m>>c)
    
{
        
if(n==0&&m==0&&c==0)
            
break;
        cout
<<"Sequence "<<k++<<endl;
        flag
=false;max=now=0;
        memset(use,
false,sizeof(use));
        
for(i=1;i<=n;i++)
            cin
>>cost[i];
        
while(m--)
        
{
            cin
>>x;
            
if(use[x])
            
{
                use[x]
=false;
                now
-=cost[x];
            }

            
else
            
{
                use[x]
=true;
                now
+=cost[x];
                
if(now>max) max=now;
                
if(now>c) flag=true;
            }

        }

        
if(flag)
            cout
<<"Fuse was blown."<<endl;
        
else
            cout
<<"Fuse was not blown."<<endl
                
<<"Maximal power consumption was "<<max<<" amperes."<<endl;
        cout
<<endl;
    }

    
return 0;
}

YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

假设最多能使用的电压为C,
有n个电器(最多20个),
第i电器工作期间将消耗costi的电。
求经过m次操作(开/关不同电器,第一次为开第二次为关,默认均是关闭)后,
保险丝是否烧断(即是否超过最大限制电压),
如没有则求出最多时使用的电量。


【粗略分析】

简单的模拟。
开关操作,找最大值,判断即可。

【C++源代码】

 

#include<iostream>
#define N 21
using namespace std;

int main()
...{
    
bool use[N],flag;
    
int cost[N];
    
int k=1,n,m,c,i,x,max,now;
    
while(cin>>n>>m>>c)
    
...{
        
if(n==0&&m==0&&c==0)
            
break;
        cout
<<"Sequence "<<k++<<endl;
        flag
=false;max=now=0;
        memset(use,
false,sizeof(use));
        
for(i=1;i<=n;i++)
            cin
>>cost[i];
        
while(m--)
        
...{
            cin
>>x;
            
if(use[x])
            
...{
                use[x]
=false;
                now
-=cost[x];
            }

            
else
            
...{
                use[x]
=true;
                now
+=cost[x];
                
if(now>max) max=now;
                
if(now>c) flag=true;
            }

        }

        
if(flag)
            cout
<<"Fuse was blown."<<endl;
        
else
            cout
<<"Fuse was not blown."<<endl
                
<<"Maximal power consumption was "<<max<<" amperes."<<endl;
        cout
<<endl;
    }

    
return 0;
}

【注意事项】

※ 感觉是没有。。不小心多打了个空格浪费了一次PE……

【点评】

米有啥好说的八。。

阅读更多
个人分类: 【ACM】解题报告
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭