https://www.codechef.com/viewsolution/36076345
Why do I get a SIGABRT error here?
For now, you should probably be more concerned about why it’s giving the wrong answer for the Example Input (there’s little point in submitting a solution until - at minimum - it gives the correct answer for all provided examples).
Who knows? While debugging that, you may or may not find your answer
Not sure if this is a valid testcase (seems to meet the Constraints, at least), but consider the test input:
1
5 5
1 1 1 1 1
#include
#define ll long long int
#include
#include
using namespace std;
int main()
{
ll test_case;
cin>>test_case;
ll temp=test_case;
ll k=0;
vectorg(temp);
while(test_case)
{
ll number;
ll c=0;
cin>>number;
vectorv(number);
map<ll,ll>m;
for(ll i=0;i<number;i++){
cin>>v[i];
m[v[i]]++ ;
if( m[v[i]]==1 &&v[i]!=0)
c++;
}
g[k++]=c;
}
for(ll i=0;i<temp;i++)
{
cout<<g[i]<<"\n";
}
return 0;
}
//why i am getting SIGABRT
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile!
Also - what Problem are you trying to solve?
Why this is getting SIGABRT Error? It works fine for single test case.
problem:LOSTMAX Problem - CodeChef
solution:CodeChef: Practical coding for everyone
[simon@simon-laptop][16:29:32]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling ashutosh_6500-LOSTMAX.cpp
+ g++ -std=c++14 ashutosh_6500-LOSTMAX.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
ashutosh_6500-LOSTMAX.cpp: In function ‘int main()’:
ashutosh_6500-LOSTMAX.cpp:17:23: warning: conversion to ‘std::__debug::multiset<int>::value_type {aka int}’ from ‘long long int’ may alter its value [-Wconversion]
s.insert(x);
^
ashutosh_6500-LOSTMAX.cpp:19:40: warning: conversion to ‘std::__debug::multiset<int>::key_type {aka int}’ from ‘std::__cxx1998::multiset<int, std::less<int>, std::allocator<int> >::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
s.erase(s.lower_bound(s.size() -1));
~~~~~~~~~^~
ashutosh_6500-LOSTMAX.cpp:10:21: warning: unused variable ‘n’ [-Wunused-variable]
long long int t,n,i,ma,x;
^
ashutosh_6500-LOSTMAX.cpp:10:23: warning: unused variable ‘i’ [-Wunused-variable]
long long int t,n,i,ma,x;
^
ashutosh_6500-LOSTMAX.cpp:10:25: warning: unused variable ‘ma’ [-Wunused-variable]
long long int t,n,i,ma,x;
^~
+ set +x
Successful
[simon@simon-laptop][16:29:36]
[~/devel/hackerrank/otherpeoples]>echo "3
1 2 1
3 1 2 8
1 5 1 4 3 2" | ./a.out
/usr/include/c++/7/debug/multiset.h:328:
Error: attempt to erase from container with a past-the-end iterator.
Objects involved in the operation:
sequence "this" @ 0x0x7ffe3dfb4f60 {
type = std::__debug::multiset<int, std::less<int>, std::allocator<int> >;
}
iterator "__position" @ 0x0x7ffe3dfb4f00 {
type = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<int>, std::__debug::multiset<int, std::less<int>, std::allocator<int> > > (mutable iterator);
state = past-the-end;
references sequence with type 'std::__debug::multiset<int, std::less<int>, std::allocator<int> >' @ 0x0x7ffe3dfb4f60
}
Aborted (core dumped)
Ohhh.Thanks
int *ar = new int(N);
creates a single int
, with value N
, on the heap. You probably meant:
int *ar = new int[N];
(yet another reason to use vector
s :))
thanks , well i got that my self by trail and error . and now i am stuck again as the answer is wrong CodeChef: Practical coding for everyone
Here’s a random test input for which your solution fails:
1
5 5 3 4 2 58 72 72
-65 -64 -61 -29 92
I am getting SIGABRT i tried a lot of different ways to fix still i cant fix it
#include
#include
#include
#include
#include
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
long long t,n,k,l;
cin>>t;
map<long long,long long>player;
while(t--){
player.clear();
cin>>n>>k>>l;
if((n-k)==l){
cout<<-1<<endl;
continue;
}
vector<long long> vi(n);
for(int i=1;i<=k;i++){
vi[i]=i;
player[i]++;
}
int p=k+1;
for(int i=p;i<=n;i++){
for(int j=1;j<=k;j++){
if(player[j]<l && (i!=j+1) ){
vi[i]=j;
}
}
}
for(int i=1;i<=vi.size();i++){
cout<<vi[i]<<" ";
}
cout<<endl;
}
}
Here’s your solution, just copy pasted from your submission.
I guess I found the error.
You are trying to access invalid memory location. (Mentioned ‘here’ as a comment).
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,n,k,c=0;
cin >> n >> k;
string s[k+1]; int a[k+1]={0};
fflush(stdin);
for(i=1;i<=k;i++)
getline(cin,s[i]);
for(i=1;i<=k;i++){
if(s[i]== "CLOSEALL"){
a[k+1]={0}; //here
cout << 0 << endl;
}
else {
if(a[stoi(s[i].substr(6,7))]==0){
a[stoi(s[i].substr(6,7))]=1;
++c;
}
else{
a[stoi(s[i].substr(6,7))]=0;
--c;
}
if(c<0) c=0;
cout << c << endl;
}
}
return 0;
}
I tried to run it by commenting that part and I still get the error.
In that part I am just Initalizing. Here is the more elaborate error
“terminate called after throwing an instance of ‘std::out_of_range’
what(): basic_string::substr: __pos (which is 6) > this->size() (which is 0)”.
thanks bro
Thank you