Skip to content

Well! You CAN Learn Something New Every Day!

June 14, 2010

I just solved the Sphere Online Judge’s “Enormous Input” test. It’s only a tutorial test, so it should have been easy. And it WOULD have been, if I had known a bit more about the merits of the iostream and stdio.h methods of reading and writing data.

The test itself is pretty basic:

The purpose of this problem is to verify whether the method you are using to read input data is sufficiently fast to handle problems branded with the enormous Input/Output warning. You are expected to be able to process at least 2.5MB of input data per second at runtime.
Input

The input begins with two positive integers n k (n, k<=10^7). The next n lines of input contain one positive integer ti, not greater than 10^9, each.
Output

Write a single integer to output, denoting how many integers ti are divisible by k.

I started off using iostream data input and output. It didn’t work. I tried saving computation time by writing to an array before computing and later to a vector, both with no luck. Then reviewing a different problem, I read this:

Guys, even if any of those hideous solutions work, which is a lot to say for the very first and easiest problem ever to exist, they will land you a time of 0.01.
The I/Ostream library is SLOW as hell – depending on what you read/write to the stream, they are at LEAST 10 times slower than the standart reading/writing functions in stdio. Use stdio.h.

And I had my solution! I went back to my first solution, put in include , substituted scanf and printf for cin and cout and submitted my code. And it worked!

Here is what I submitted:
#include
int main()
{
int n;
int k;
int count = 0;
int l;
scanf(“%d”, &n);
scanf(“%d”, &k);
for (int i = 0;i<n;i++)
{
scanf(“%d”, &l);
if(l%k==0)
{
count++;
}
}
printf(“%d\n”, count);
return 0;
}

clean, simple and most importantly, successful! I’m happy!

Advertisements

From → Code

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: