问题描述
以下代码在我的 DEV-C++ 编译器中完美运行,但是当我在 codechef 中提交时,运行 3-4 秒后显示SIGABRT ERROR".我已经研究了这个错误,并尽我所能调试,但即使一周后我也无法调试.请帮忙 !!提前致谢.
The following code ran perfectly in my DEV-C++ compiler but when I submitted in codechef, after running for 3-4 seconds it shows "SIGABRT ERROR". I have researched on this error and have done everything i could to debug, but even after a week I am not able to. Please Help !! Thanks in advance.
参考问题是http://www.codechef.com/problems/LOWSUM
enter code here
void selsort(long long *ssum,long long len)
{
long long low;
for(long long i=0;i<len;i++)
{
low = ssum[i];
long long pos=i;
for(int j=i+1;j<len;j++)
{
if(ssum[j]<low)
{
low = ssum[j];
pos = j;
}
}
ssum[pos] = ssum[i];
ssum[i] = low;
}
}
int main()
{
int t,k,q;
cin>>t;
for(int i=0;i<t;++i)
{
cin>>k;
cin>>q;
long long sq = k*k;
long long *mot=NULL,*sat=NULL;
mot = new long long [k];
sat = new long long [k];
long long *sum = new long long[sq];
long long qth;
long long b=0;
for(int j=0;j<k;++j)
{
cin>>mot[j];
}
for(int j=0;j<k;++j)
{
cin>>sat[j];
}
for(int j=0;j<k;++j)
{
for(int a=0;a<k;++a)
{
sum[b] = mot[a]+sat[j];
++b;
}
}
selsort(sum,sq);
for(int j=0;j<q;++j)
{
cout<<"\n";
cin>>qth;
cout<<"\n"<<sum[qth-1];
}
delete []sum;
delete []mot;
delete []sat;
}
return 0;
}
推荐答案
SIGABRT 信号发送的原因有很多,引用 codechef
SIGABRT signal is sent due to many reasons quoting codechef
SIGABRT 错误是由您的程序因致命错误而中止引起的.在 C++ 中,这通常是由于 C++ 中的 assert 语句未返回 true,但如果某些 STL 元素尝试存储过多内存,则可能会生成此错误.
在你的情况下,它似乎使用了过多的内存
In your case it seems to be use of excessive memory
mot = new long long [k];
sat = new long long [k];
long long *sum = new long long[sq];
请注意,k 的值可以大到 20000,因此声明一个大小为 k 的数组会很好,但是您的 sq = k*k
的数量级为 4*10^8正在导致内存不足问题内存.而且你的算法也不够好,无法在时限内给出 AC.
Note that the value of k can be as large as 20000 so declaring a array of size k will be fine but your sq = k*k
which is of order of 4*10^8 which is causing a out of memory problem memory. And your algorithm is also not good enough to give AC within time limit.
Codechef 有自己的论坛来问这些问题,并且已经讨论了解决这个问题的更好方法
Codechef has its own forum to ask such questions, and preferable ways to solve this problem has already been discussed there
http://discuss.codechef.com/problems/LOWSUM
这篇关于“Sigabrt 错误"- 编解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!