hugewarriors's blog

By hugewarriors, history, 20 months ago, In English

1801B - Buying gifts

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.

  • Vote: I like it
  • -11
  • Vote: I do not like it

| Write comment?
»
20 months ago, # |
Rev. 2   Vote: I like it +34 Vote: I do not like it

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.

  • »
    »
    20 months ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    Thanks for the insight.

  • »
    »
    20 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Please tell how do I compile using that? It is not in the language options in Custom Innvocation.

    • »
      »
      »
      20 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Add #define _GLIBCXX_DEBUG on the top of your code before #include's.

»
20 months ago, # |
  Vote: I like it +34 Vote: I do not like it

but it was the compiler

The audacity