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

Автор notTehlka, 3 года назад, По-английски

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

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

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

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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

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

Undefined behavior. Also, hacked

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

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

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

      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 года назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

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