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

Автор hugewarriors, история, 13 месяцев назад, По-английски

1801B - Покупка подарков

198392013 I got wrong while using GNU C++20 as compiler. But the same solution is accepted by GNU C++17 compiler.

198391568 This is the accepted solution using GNU C++17 compiler.

I wasted 5 to 6 hrs in searching of error, but it was the compiler. It's really frustrating while searching for errors, when there's not any.

Are the additional library in GNU C++20 causing this issue? I have used set in this problem, which seems to be not handled properly by this compiler.

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

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

Your code contains undefined behavior and you just get very lucky that it works with the c++17 compiler.

        auto it = s.lower_bound(v[i].first);
        int cur = inf;
        if(s.find(*it) != s.end()){

When s is empty (or in cases where lower_bound isn't in s) then s.lower_bound returns s.end() and *it is undefined.

I'm not really sure what you are doing here — you've already got an iterator pointing to that value so dereferencing it to call find doesn't do anything. s.find(*it) will always just return it (except for when *it is undefined).

Compiling with -D_GLIBCXX_DEBUG highlights your error as soon as you run the code.

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

but it was the compiler

The audacity