Code not working

Asked

Viewed 56 times

0

I’m trying to solve a problem of the site Urionlinejudge (problem link) and I’m getting a mistake like Time limit exceeded, and I’m having trouble identifying the problem. I can imagine that it is in the first scanf(), where it may be necessary to read the input without using the ENTER key. Below is the code.

#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);
            }
        }
    }
}

1 answer

1


The execution time of your code is exceeding the specified limit of the problem.

Its code is demanding a lot of execution time because of the internal loops, the ideal was to avoid using internal loops.

You can try using struct to try to decrease the execution time of the code.

For example:

Implement each message as a point

struct ponto {
  int x;
  int y;
  int n; //o numero de schweisen
};

and store each point in a Chained List.

Browser other questions tagged

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