poj 1484 Blowing Fuses

it2024-03-29  13

Blowing Fuses

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 2584 Accepted: 1029

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.

 

1: #include<iostream> 2: using namespace std; 3: int main() 4: { 5: int n,m,c; 6: int consumption[30]; 7: int flag[30]; 8: int i,j; 9: int sum; 10: int operation; 11: int cases=0; 12: int burned; 13: int max_sum; 14: // freopen("E:\\c++\\oj\\t.txt","rt",stdin); 15: while(1) 16: { 17: cin>>n>>m>>c; 18: if(n==0&&m==0&&c==0) 19: break; 20: cases++; 21: for(j=1;j<=n;j++) 22: cin>>consumption[j]; 23: sum=0; 24: burned=0; 25: max_sum=0; 26: memset(flag,0,sizeof(flag)); 27: for(i=0;i<m;i++) 28: { 29: cin>>operation; 30: if(burned==1) 31: continue; 32: if(flag[operation]==0) 33: { 34: flag[operation]=1; 35: sum+=consumption[operation]; 36: if(sum>c) 37: { 38: cout<<"Sequence "<<cases<<endl; 39: cout<<"Fuse was blown."<<endl<<endl; 40: burned=1; 41: } 42: else if(sum>max_sum) 43: { 44: max_sum=sum; 45: } 46: } 47: else 48: { 49: sum-=consumption[operation]; 50: flag[operation]=0; 51: } 52: } 53: if(burned==0) 54: { 55: cout<<"Sequence "<<cases<<endl; 56: cout<<"Fuse was not blown."<<endl; 57: cout<<"Maximal power consumption was "<<max_sum<<" amperes."<<endl<<endl; 58: } 59: } 60: return 0; 61: } 62: 63:

转载于:https://www.cnblogs.com/w0w0/archive/2011/11/21/2257541.html

相关资源:数据结构—成绩单生成器
最新回复(0)