SPOJ returns "Wrong answer" to seemingly correct solution

Asked

Viewed 172 times

2

I tried to solve the problem BANCO12 - Bank SPOJ, but when submitting my C++ code, the system returns "wrong answer". Follow my code: `

#include <iostream>
using namespace std;

int main(){
    int nc, np, i, c, livre=0, cont=0;
    cin>>nc>>np;
    int caixa[nc], espera[np], chegada, demora;

    for(i=0; i<nc; i++){
        caixa[i]=0;
    }

    for(i=0; i<np; i++){
        cin>>chegada>>demora;
        espera[i] = caixa[livre]-chegada;
        if(espera[i]>20){cont++;}
        caixa[livre]+=demora;

        if(nc>1){
            for(c=0; c<nc; c++){
                if(caixa[livre]>caixa[c]){
                    livre = c;
                }
            }
        }
    }
    cout<<cont<<endl;
    return 0;
}

I don’t know where the mistake is, can you help me?

  • remove the line cout<<espera[i]<<endl;, your code has several exits and the site requires only the result.

  • Sorry, I used this to make a test but forgot to withdraw, even without this line giving error. Still thank you!

  • can be answer line break, try to use only cout << cont;

  • No, it was missing a test case, when the arrival is bigger than the free cash, I managed to solve. Thanks for the help!

1 answer

0


I found the solution, the code was not considering the case where the arrival is bigger than the free box. So just add this line of code: if(chegada>caixa[livre]){caixa[livre]=chegada;}, before calculating the wait. The final code is this way:

#include <iostream>
using namespace std;

int main(){
    int nc, np, i, c, livre=0, cont=0;
    cin>>nc>>np;
    int caixa[nc], espera[np], chegada, demora;

    for(i=0; i<nc; i++){
        caixa[i]=0;
    }

    for(i=0; i<np; i++){
        cin>>chegada>>demora;
        if(chegada>caixa[livre]){caixa[livre]=chegada;}
        espera[i] = caixa[livre]-chegada;
        if(espera[i]>20){cont++;}
        caixa[livre]+=demora;

        if(nc>1){
            for(c=0; c<nc; c++){
                if(caixa[livre]>caixa[c]){
                    livre = c;
                }
            }
        }
    }
    cout<<cont<<endl;
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.