[英]Optimization of C++ code
This is my solution to Spoj 11373. Coke madness 这是我对Spoj 11373的解决方案。 可乐的疯狂
#include <cstdio>
int main(){
int T; scanf("%d",&T);
for(int j = 1; j <= T; j++){
int n;
scanf("%d",&n);
long long sum = 0, small = 0;
int t;
for(int i = 0; i < n; i++) {
scanf("%d",&t);
sum += t;
if( sum < small) small = sum;
}
printf("Scenario #%d: %lld\n", j, -1*small+1);
}
}
The problem is simple. 问题很简单。 This solution takes 0.12 seconds on Spoj, though there are 0.01 second solutions.
尽管有0.01秒的解决方案,但此解决方案在Spoj上花费的时间为0.12秒。 I am curious what optimizations may be done to this code to get faster.
我很好奇可以对此代码进行哪些优化以使其更快。 I thought
-1*small+1
may be got using bit-wise operations but didn't find how. 我认为
-1*small+1
可以使用按位运算获得,但没有找到具体方法。 Also I can't get rid of long long since sum may exceed int. 我也不能摆脱长久,因为总和可能超过int。
You're making way too many I/O calls. 您正在进行太多的I / O调用。 Read the whole file at once, and then parse it, then create your output, and then write it all at once.
一次读取整个文件,然后对其进行解析,然后创建您的输出,然后一次将其全部写入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.