notTehlka's blog

By notTehlka, 5 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

»
5 years ago, hide # |
 
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;
»
5 years ago, hide # |
 
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...

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

Undefined behavior. Also, hacked

»
5 years ago, hide # |
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