Problem with lunchtime piece of cake

bug
debugging
easy
lunchtime

#1

My code for today’s lunchtime problem piece of cake can be found here:
http://www.codechef.com/viewsolution/5991738

It gives me WA though I myself believe that it’s correct. I’ve already tried all possible cases I could think of, and my code works correctly for all of them.Ill be thankful if anyone can help me to find the bug in this code…


#2

As the name suggests, ios::sync_with_stdio(false) stops syncing iostreams (cin and cout) with stdio (scanf and printf)

You should do one of the following:

  1. Use only scanf/printf and not cin/cout. In this case sync_with_stdio is of no purpose. The execution time will be the same whether you use it or not.
  2. Use both, but remove the sync_with_stdio line (this may result in TLE, not recommended)
  3. Use only cin/cout. You can keep the sync_with_stdio line and it will be fast enough. (if you remove it and use cin/cout you may get TLE)

#3

Hello nibnalin,

Your code is all correct …
just comment this line … ios_base::sync_with_stdio(false); and boom you sill surely get AC. I also faced same problem long time ago … both (cin /cout) and scanf/printf does not work correctly in presence of this statement …


#4

@nibnalin :
i hope the link below from codeforces blog will help you regarding this line for enormous input.
From my experience what i have found is that u get correct results if u use only cin and cout with that line.
http://www.codechef.com/viewsolution/5995741 is the accepted solution… this is your code but with slight modifications … what i have done is that i have used cin and cout instead of scanf and printf
http://codeforces.com/blog/entry/925


#5

But my code compiles and runs within the time limit
. Though the info you gave is useful, yet that doesn’t justify a WA…


#6

nice style of coding … :smiley:


#7

@ma5termind I hate it when these kinds of things happen, though I’m not sure why both of them won’t work correctly. They work on my machine…


#8

It compiles and runs within the limit, yes.

Sorry, I didn’t explain WHY you should do what I said. Your program doesn’t take the input correctly, because you are using both scanf and cin when they aren’t synced with each other. They aren’t synced because you’ve disabled syncing with sync_with_stdio(false). So your output is wrong, which is why you’re getting wrong answer.

If it didn’t compile correctly, you would get a compile error.
If it exceeded the time limit, you would get “time limit exceeded”.

You’re getting wrong answer because you program produces incorrect output.