-1

I Read all about the reasons for this error(SIGABRT) -> made this program using arrays and that gave me SIGSEGV. Please show me where have I asked for some memory which compiler cannot allocate or narrow it down to exact problem and where it resides??
Constraints are described below, and I've used long long int

  1. 1 ≤ T ≤ 100
  2. 1 ≤ N, K ≤ 100,000
  3. 1 ≤ sum of K over all test cases ≤ 100,000
  4. 1 ≤ Ai, Di ≤ 1,000,000 for each valid i
  5. 1 ≤ Bi ≤ C for each valid i
  6. Bi+1 < Bi for each valid i

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long int ll;
    
    
    int main() {
    ll T;
    cin>>T;
    
    while(T--){
    
        ll N,K;
        cin>>N>>K;
    
        vector<ll> A(N,0);
        vector<ll> D(N,0);
        vector<ll> B(N,0);
    
        for(ll i=0; i<N ; i++){             // O(N)
            cin>>A[i];
        }
    
        for(ll i=0; i<N ; i++){             // O(N)
            cin>>D[i];
        }
    
        for(ll i=0; i<K ; i++){             // O(K)
            cin>>B[i];
        }
    
        ll vect_size=0;
    
        for(ll i=0; i<N ; i++)              // O(N)
            vect_size += D[i] ;
    
        //cout<<vect_size<<endl;
    
        vector<ll> cards(vect_size,0);
        ll sum=0;
    
    
        for(ll i=0; i<N ; i++){
    
            for(ll j=0; j<D[i] ; j++){
    
                cards[ sum+j ] = A[i] ;
            }
            sum+=D[i];
        }
    
    
       //sort(cards, cards+vect_size );                    // O(N.logN)  ::  N = vect_size
       sort(cards.begin(), cards.end());
    
        //----------------------game-begins-now-----------------------------------------
    
    
    
        ll left_lim=0;
        ll right_lim=vect_size-1;
        ll cur_size=vect_size;
    
        for(ll i=0; i<K; i++){
    
            if( i%2==0 ){
    
                    left_lim += cur_size-B[i] ;
                    cur_size  = B[i];
    
            }
            else{
    
                    right_lim -= cur_size-B[i];
                    cur_size  = B[i];
            }            
    
    
    
    
        }
    
        sum=0;
    
    
    
        for(ll i=left_lim; i<=right_lim; i++)
                sum += cards[i];
    
        cout<<sum<<"\n";
    
    
    }
    
    
    return 0;
    

    }

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • Unrelated to your problem, but please take some time to read [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Also please avoid creating "shortcuts" of common types using `typedef`, it makes the code harder to read and understand. – Some programmer dude Feb 26 '18 at 06:53
  • 1
    Much more related to your problem, please take some time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Being able to debug your programs is a required skill for any programmer on any level. – Some programmer dude Feb 26 '18 at 06:55
  • You probably just ran out of memory due to an infinite loop, or due to inefficient handling of a very large input. Those sites tend to have good test cases, so they found a bug in your code, but asking us defeats the purpose of learning or competing. – Kenny Ostrom Feb 26 '18 at 06:57
  • A possible problem is that you use `N` as the size for `B`, but use `K` as the limit for `B[i]`. – Bo Persson Feb 26 '18 at 06:58
  • thanks man, really needed such a thing. If there a tutorial available somewhere, I am sure it would be of great help to us newbies. – Tanmay Gautam Feb 26 '18 at 07:01
  • 1
    Just looking at the limits: if `D[i]` is 1,000,000 for all `i`, and `N` is 100,000, `vect_size` will be one hundred billion. I suspect you need a cleverer algorithm. – molbdnilo Feb 26 '18 at 07:01
  • @Bo Persson, i did that using K now, still it's the same, but yeah thanks I'll change that now. – Tanmay Gautam Feb 26 '18 at 07:04
  • @molbdnilo do you think making a multimap of will improve complexity?? current time complexity is O(N.logN) with N = vect_size – Tanmay Gautam Feb 26 '18 at 08:41
  • You just need a sorted sequence of pairs of (Ai, Di). If you played this in the real world, you would most likely keep piles of similar-valued cards rather than spreading them all out and counting them. Do that in the program. – molbdnilo Feb 26 '18 at 09:40

1 Answers1

1

I am not clear what is the problem you are having (e.g., which line throws the error), but this can get you started:

  1. Use std::vector<ll>::max_size() to get the maximum allowable size. This is possibly 2^30-1. Check if you are exceeding that.

  2. If N < vect_size, line cards[ sum+j ] = A[i] is a problem.

  3. vect_size can be up to LONG_MAX * LONG_MAX (see climits). LONG_MAX is possibly 2^31-1 or greater, so LONG_MAX * LONG_MAX may be approx. 2^62, which conceivably exceeds the limit in point 1. This is probably not what you want.

  4. Depending on the specific contents of your vectors, the only other possibly problematic line is sum += cards[i].