> Usually, when dealing with millions of I/O operations, the bottleneck is stream flushing.
> std::endl flushes the stream every time, so try replacing endl with "\n" and see if that helps.
there is only one output operation...
for the inputs, may try
std::ios_base::sync_with_stdio(false);
at the beginning.
http://codeforces.com/blog/entry/925
> it sounds like the enqueueing and dequeuing is the bottleneck.
perhaps you should show the implementation of those function (and the `queue' class).
or simply use a stl container.
> I think you should be able to change the logic around to only use 1 queue
>> You do not need two containers just one.
the second container would have at most one element.
(dunno how removing one container may speed up anything)
> it still doesn't run in less than a second. How can i improve that?
if `.enqueue()' and `.dequeue()' are O(1) then your algorithm is O(n)
so aside from lastchance's improvement, there is no much you can do.
consider buying a better computer.
(you are compiling with optimizations, ¿right?)