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

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

I was solving 1955G - GCD on a grid, and I made 2 submissions. The first one 262439330 which gave TLE. The second one 262487569 ran in just 765ms.

The only change I made in these 2 submissions is that I declared a vector isposs globally instead of declaring it again in the function isPoss. Does memory allocation in C++ really have such a large overhead, or is there any other reason? I mean it takes more than 4x time to run, which is unexpected to me.

 Link of diff: https://www.diffchecker.com/vVXUaFzd/

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

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

because of the code logic is TLE. Opening an array is generally of a linear complexity rather than O(1). It has nothing to do with being global or not. When n and m are large enough, your tle code is equivalent to doing an additional traversal each time.

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

    Allocating an array space and initializing it is always O(n). The correct way to write it should be your global writing method, or.

    #include <xxx>
    int solve() {
      vector<vector<int>> yourvector(n, vector<int>(m,0))
      fution<> useYourVector() -> bool {
        cleardata()
        then use vector
      };
      return; 
    }
    
    int main() {
     solve()
    }
    
  • »
    »
    5 недель назад, # ^ |
      Проголосовать: нравится +11 Проголосовать: не нравится

    Yes I know memory allocation is not O(1). But since the function is anyways O(n*m) it should not make much of a difference to create a vector of dimensions n*m. I have made a small update, I hope that makes it more clear what I am trying to say.

»
5 недель назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

Your second version differs from the first one not only in moving the isposs vector to the global namespace. Namely, in the first version you initialized isposs with default values every call while in the second version you used resize() that does not re-assign default values to existing elements, only to ones in extension. Also there are some assignments to elements of isposs missing in the first version (maybe they change time complexity, I do not know).

  • »
    »
    5 недель назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    Please take a look at this submission 262487569. The difference of the first one is that I have kept isposs global, and I am assigning default values each time in isPoss function.

    • »
      »
      »
      5 недель назад, # ^ |
        Проголосовать: нравится +10 Проголосовать: не нравится

      Yes, it seems like slow memory allocator. Besides, I changed the TLEed version (replaced vector with array) and it passed tests: 262492049.

      • »
        »
        »
        »
        5 недель назад, # ^ |
          Проголосовать: нравится +5 Проголосовать: не нравится

        Wow that's surprising, it probably means vector allocation is slow in C++. If I find an answer to why this is happening I'll update it here.

        • »
          »
          »
          »
          »
          5 недель назад, # ^ |
            Проголосовать: нравится +15 Проголосовать: не нравится

          Here is the TLEed version with 1D-vector: 262495379 (also passes tests). You can make some conclusions :)

        • »
          »
          »
          »
          »
          4 недели назад, # ^ |
            Проголосовать: нравится +4 Проголосовать: не нравится

          A vector initializes each element upon creation, while an array keeps the memory uninitialized (unless you explicitly initialize it). Usually this difference is negligible, but they are surely different.

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

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

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

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

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

Reference — link