Time Limit Exceeded

Asked

Viewed 155 times

1

I wrote the code below to solve the problem 1112 from the site Urionlinejudge and apparently the logic employed is perfect, but I’m getting an error of time exceeded. I’m having trouble identifying the error. It appears to be in the first scanf(), since when I run the program I can type an endless sequence of 0’s and spaces that the program will only return zero and end when the ENTER key is pressed, but I don’t know what to do for it, when you read 3 consecutive zeroes in the first entry, now close. I believe that it is necessary that, when reading these three data, even without pressing the ENTER key, the program already closes if there are three zeros.

#include <stdio.h>

int main (){
    int X, Y, P, Q;
    int i, j, k, l;
    char carac;
    while(1){
        scanf("%d %d %d", &X, &Y, &P);
        if(X==0 && Y==0 && P==0){
            return 0;
        }
        int campo[X][Y];
        for(i=0;i<X;i++){
            for(j=0;j<Y;j++){
                campo[i][j]=0;
            }
        }
        scanf("%d", &Q);
        int N=0, Z, W, qtd;
        for(i=0;i<Q;i++){
            qtd=0;
            scanf(" %c", &carac);
            if(carac=='A'){
                scanf("%d %d %d", &N, &X, &Y);
                campo[X][Y]+=N;
            }
            else{
                scanf("%d %d %d %d", &X, &Y, &Z, &W);
                for(j=X;j<=Z;j++){
                    if(Y>W){
                        l=Y;
                        Y=W;
                        W=l;
                    }
                    for(k=Y;k<=W;k++){
                        qtd+=campo[j][k];
                    }
                }
                printf("%d\n", qtd*P);
            }
        }
    }
}
  • I opened the link, took a look at the problem and noticed that at the top of the page there is the indication of a maximum time limit: "timelimit 4". That is, probably, the problem is not in the code and you have simply run out of time to present the solution.

1 answer

2

Checks the amount returned by scanf()

        if (scanf("%d%d%d", &X, &Y, &P) != 3) exit(EXIT_FAILURE);

        if (scanf("%d", &Q) != 1) exit(EXIT_FAILURE);

            if (scanf(" %c", &carac) != 1) exit(EXIT_FAILURE);

                if (scanf("%d%d%d", &N, &X, &Y) != 3) exit(EXIT_FAILURE);

                if (scanf("%d%d%d%d", &X, &Y, &Z, &W) != 4) exit(EXIT_FAILURE);

Before the exit(EXIT_FAILURE) can send an informative message to stderr, for example: fprintf(stderr, "Erro de input na linha %d\n", __LINE__);

  • That still doesn’t solve the problem. I need the user to type "0 0 0" in this entry, without pressing ENTER after that, the program already recognizes the three values. The only problem is that scanf() is waiting for a user ENTER, which will not have in this case.

  • The input is "line buffered"; which means that the operating system (more properly the library Runtime do C) only releases the data together with a ENTER. There is no (easy) return to this situation. I do not believe that the online Judge is counting on programs that work without ENTER.

Browser other questions tagged

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