notTehlka's blog

By notTehlka, 3 years ago, In English

My submission 114580614 and submission 114581483 for problem D educational round 108 gave runtime error in C++17 and C++14 respectively whereas submission 114582152 got accepted in C++11. Can someone explain?

UPD: I think there was some problem in the line 49-50 because it may be possible that length of prof < n, but why did the code work in C++11 ??

for (int i=0;i<n;i++) prof[i] = 0;

Thanks

  • Vote: I like it
  • +15
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Change this:

    for (int i=0;i<n;i++)
        prof[i] = 0;

to this:

    for (int i=0;i<prof.size();i++)
        prof[i] = 0;
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Whoa, your runtime error codes now gives AC in c++17 now. This is really strange...

»
3 years ago, # |
  Vote: I like it +34 Vote: I do not like it

Undefined behavior. Also, hacked

  • »
    »
    3 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    What test case did you use to hack? And why did the code pass pretests in the first case?

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it +13 Vote: I do not like it

      I hacked it with simple case of

      1
      1
      1
      

      You are accessing out of bounds. It is an undefined behavior. Hence, unpredictable results.

»
3 years ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

Undefined behaviour:

    for (int i=0;i<n;i++)
        prof[i] = 0;

as here it is not necessarily has a length of n

Let's say that n=4

    for (int win=4;win<n+1 /* 4 < 4+1 */;win+=2)
    {
        // win = 4
        vector<ll> prof1;
        for (int i=0;i<n+1-win;i++) // 0 < 4+1-4
        {
            // ...
            prof1.push_back(x);
        }
        prof = prof1;
        // prof has length of 1
    }

So the runtime error is correct expected behaviour as your array has length less than n