Topic links: http://poj.org/problem?id=1484
This problem can be straightforward simulation. You n containers, m operations, maximum capacity C. Each of the switching operation of the analog device. If the original is closed, then open, while the maximum power consumption plus the power consumption of the device. If the original is open, shut it, while the current value minus the power consumed by the device. The maximum power consumption up to date. If greater than C, the output Fuse was blown. Otherwise, the maximum power output is reached. The simulation algorithm is very simple, but I WA for a long time (..........). until
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cmath> 5 #include<vector> 6 #include<stack> 7 #include<map> 8 #include<set> 9 #include<list> 10 #include<queue> 11 #include<string> 12 #include<algorithm> 13 #include<iomanip> 14 using namespace std; 15 #define MAX 50 16 17 struct node 18 { 19 int sta; 20 int power; 21 }; 22 23 node arr[MAX] ; 24 25 int n,m ,c; 26 27 int main() 28 { 29 int p = 1; 30 while(cin>>n>>m>>c && n!= 0 && m!= 0 && c != 0) 31 { 32 int t = 0;//功耗警告 33 int sum = 0; 34 int max = 0 ; 35 int X; 36 for ( int I = 0 ; I <n-; I ++ ) 37 [ { 38 is CIN >> ARR [I] .Power; 39 ARR [I] .STA = 0 ; // initially closed state 40 } 41 is for ( int I = 0 ; I <m; I ++ ) 42 is { 43 is CIN >> X; 44 is IF (ARR [X- . 1 ] .STA == 0 )// current in the closed state 45 { 46 is ARR [X- . 1 ] = .STA . 1 ; // Open 47 SUM = + ARR [X- . 1 ] .Power; 48 IF (SUM> max) {max = SUM;} 49 IF (SUM> C) 50 { 51 is T = . 1 ; 52 is // BREAK; // add this will always WA, WA 53 is } 54 is } 55 the else // currently open 56 { 57 arr[x-1].sta = 0; 58 sum -= arr[x-1].power; 59 } 60 } 61 if(t == 1) 62 { 63 cout<<"Sequence "<<p++<<endl<<"Fuse was blown."<<endl; 64 } 65 else 66 { 67 cout<<"Sequence "++ P << << endl << " . Not blown Fuse WAS " << endl << " Maximal Power consumption WAS " << << max " Amperes. " << endl; 68 } 69 COUT << endl; // NOTE format requirements, without this will wrap the PE 70 } 71 is return 0 ; 72 }
When the judge between power consumption is greater than C, I would flag is set to 1, direct break out, resulting in WA for a long time, in fact, delete it (should not have to add the phrase). Because the meaning of Title m reads all operations before final determination. Halfway break out, although the logic there is nothing wrong, but the latter did not read the actual data, the program does not allow this.
Finally, the title of the output format should pay attention to the next, there is a line spacing between each Sequence