Time limit exceeded, I need clarification please

Good morning, I want to know why my solution for the problem TWODOGS is working in 1.01 seconds and gets a TLE where there are successful submissions with time > 10 seconds ??
I need to know it there’s something I am getting wrong about the rules.
P.S: I am new to codechef
Thank you :slight_smile:

Hi and welcome in CodeChef community :wink:

The reason is that timelimit is 1 second for test input file. So if you cannot process the file in 1s, you will get TLE (your program is killed).

Typical reason is that your algorithm complexity is bad. On the other hand one of bad habbits newcommers are doing is waiting for key press at the end of program, that can also lead to TLE.

2 Likes

Thank you for your answer. But then I have another question concerning the Enormous Input problems. Is there a way to read data without cin/scanf? A faster way?

int scan()
{
    int noRead=0;
    char p= getchar();
    for(;p<33;)
    {
     p=getchar();
    };
    while(p>32)
    {
     noRead = (noRead << 3) + (noRead << 1) + (p - '0');
     p=getchar();
    }
    return noRead;
};
1 Like

Hello @ghaieth,

You can refer to @sparshgunner12 answer to know about a faster way to read and process I/O.

However, I must tell you that if your program is getting TLE with the usage of cin/scanf, that possibly means that it’s the algorithm which is delaying the whole program and not the I/O operations.

This means that if a particular program needs to do, say, 10^7 operations in 1 second, you can do it using only cin/cout or scanf/printf if your algorithm complexity is O(N).

However, trying to use fast I/O to “force” a O(N^2) approach to pass, will never do and you need a better algorithm! (yes, this just happened/is happening to me with TWODOGS problem xD)

Best,

Bruno

2 Likes

And, adding to @betlista explanation, there are successful submissions with time > 10sec because usually there are many test case files, so, if your program takes 0.5s to process one input file and there are 20 input files, time shown will be 0.5 x 20 = 10 sec

2 Likes

Thanks, I forgot to add this info :wink:

1 Like

you can use getchar_unlocked instead of getchar.

1 Like