TheBhediyaOfDalalStreet's blog

By TheBhediyaOfDalalStreet, history, 23 months ago, In English

Pardon my choice for variable naming (I was projecting), but I fail to understand why the same code gives me WA with G++20 but AC with G++17

G++20: https://codeforces.net/contest/1768/submission/188128207 G++ 17: https://codeforces.net/contest/1768/submission/188129998

Further, on debugging with G++20 for the case:

1
2
2 2

I found that mysteriously, a[0] becomes equal to 1 after the following for loop from the code is executed:

        for(int i = 1; i <= n; i ++) {
           if(cnt[i] > 2) {
                pos = 0;
                break;
            }
            if(cnt[i] == 2) {
                if(no_bitches.empty() || *no_bitches.begin() > i) {
                    pos = 0;
                    break;
                }
                dual[i] = *no_bitches.begin();
                no_bitches.erase(no_bitches.begin());
            }
        }

You can try it on custom invocation too. I don't change the input array a after taking in input, so I don't know how that happens... Can anyone explain?

»
23 months ago, # |
  Vote: I like it +4 Vote: I do not like it

I hope someday there will be hint on topic creation like "if you have strange results in different compilers then vveeeeeeerrryyyyy most likely you've got undefined behaviour and examples of it"

int dual[n] = {},  ;

for (int i = 1; i <= n; i++) {
    ...
    dual[i] = *no_bitches.begin();
}

There is no dual[n], so it seems that array a is allocated right after array dual, so dual[n] is reference to a[0] actually.

»
23 months ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it

You got ac out of luck. Main problem is you are accessing nth idx where dual have a size of n. Just increase the dual array size.