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

Автор s3yoonpark, история, 17 месяцев назад, По-английски

So I am solving question 3 from here https://www.olympiad.org.uk/papers/2016/bio/bio16-exam.pdf

I have two pieces of code, one in Python and one in C++. C++ -> https://pastebin.com/bHkhZcZ6 Python -> https://pastebin.com/8G4BZQx6

Although the both pieces of code seem to be employing the same logic, the Python one seems to be a lot faster than C++.

An example test case could be -> 614700 3643 90149 -> (It should output 18)

Please can someone tell me why this is the case?

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

»
17 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by s3yoonpark (previous revision, new revision, compare).

»
17 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by s3yoonpark (previous revision, new revision, compare).

»
17 месяцев назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

vector in c++, set in python.

You can delete this blog.

  • »
    »
    17 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    That makes that much of a huge difference? What data structure should be used in c++ then? I already tried using a set, there is no notable performance improvement. https://pastebin.com/F6WBh0YY

    • »
      »
      »
      17 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      c++ std::vector is a dynamically resized contiguous array.

      your find() operation is O(N) for a length-N vector.

      python set is a hash-set.

      your in operation is O(1) for a size-N set.

      there are more nuances to the time complexity of hashing and collisions but for your purposes, yes the lookup for a hashset is much faster.

      • »
        »
        »
        »
        17 месяцев назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        Oh I see why it would make a difference.

        Then, how should I modify my C++ code so that it runs quickly?

        EDIT: I used an unordered_set and it worked. Thanks a lot!