Блог пользователя BerryYoghurt

Автор BerryYoghurt, история, 6 лет назад, По-английски

Hello, I've recently solved this problem and here is my solution. (Takes 1575 ms in the worst case) Since I am new to cp I decided to look around in the other submissions to see how different the execution times are and whether there are more efficient algorithms. I found many submissions which solve the problem in under 300 ms, but one was particularly surprising, namely because the algorithm is almost identical to mine!

I can, however, spot three main differences. First, the other solution uses int for all the values, whereas mine uses long or long long depending on the range. I tried changing all to int in my code but as I had expected it gave wrong answers for large numbers.. I can't understand why it works in the other solution.. Second, the other solution uses C i/o (i.e. printf and scanf) whereas I use cin and cout. Third, the other solution doesn't initialise any variables, but I assume iirc they're automatically initialised? Either way, for the sake of correctness, they have to be initialised, so this point shouldn't cause the time difference.

So my question is, to sum up: what is the quintessential difference that causes this colossal difference? Surely "different CPUs" should be out of question since it's all Codeforces servers, no?

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +8 Проголосовать: не нравится

Try adding ios_base::sync_with_stdio(0);. 90455464

The main reason your solution is so slow is reading the input; this line speeds up cin by a lot but if you use it you can't use scanf/printf and cin/cout in the same code