You are not logged in. Please login at www.codechef.com to post your questions!

×

Problem with lunchtime piece of cake

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...

asked 25 Jan '15, 15:30

nibnalin's gravatar image

6★nibnalin
1611516
accept rate: 0%


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)
link

answered 25 Jan '15, 15:49

superty's gravatar image

3★superty
36417
accept rate: 31%

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

(25 Jan '15, 15:59) nibnalin6★

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.

(25 Jan '15, 17:43) superty3★

@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

link

answered 25 Jan '15, 17:55

saiavinashiitr's gravatar image

1★saiavinashiitr
906512
accept rate: 0%

edited 25 Jan '15, 18:02

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 ..

link

answered 25 Jan '15, 16:05

ma5termind's gravatar image

3★ma5termind
1.7k11630
accept rate: 11%

nice style of coding .. :D

(25 Jan '15, 16:05) ma5termind3★

@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...

(25 Jan '15, 16:12) nibnalin6★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3,703
×591
×520
×56

question asked: 25 Jan '15, 15:30

question was seen: 2,926 times

last updated: 25 Jan '15, 18:02