zoj 1195 Blowing Fuses

血一样的教训:过于复杂的循环编译器无法优化,因此将IO操作分布于一个复杂的for循环中会造成性能的急剧下降

#include<iostream> #include<stdio.h> #include<math.h> using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif int n,m,c; int power[30]; int op[1000]; int on[30]; scanf("%d %d %d",&n,&m,&c); int count = 1; bool blow; while(n||m||c) { int max = 0; for(int i=1;i<=n;i++) { scanf("%d",&power[i]); on[i] = 0; } blow = false; for(int i=0;i<m;i++) { scanf("%d",&op[i]);//NEVER PUT THIS IN BELOW FOR LOOP, OTHERWISE TLE } int nowPower = 0; for(int i=0;i<m;i++) { if(on[op[i]]) { nowPower -= power[op[i]]; on[op[i]] = 0; }else { nowPower += power[op[i]]; on[op[i]] = 1; if(nowPower > c) { blow = true; break; } if(nowPower > max) { max = nowPower; } } } if(blow) { printf("Sequence %d/nFuse was blown./n/n", count++); }else { printf("Sequence %d/nFuse was not blown./nMaximal power consumption was %d amperes./n/n", count++, max); } scanf("%d %d %d",&n,&m,&c); } }  

你可能感兴趣的:(zoj 1195 Blowing Fuses)