Скоро Codeforces Round 1133 (Div. 1, Div. 2)! Регистрация здесь. ×

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

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

Hi :)

Can any body help me about problem 299 sgu !?

Why my code got Runtime Error on test 11 !! ?

What's wrong with me ?!

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

»
13 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't know, how it can be compiled, but it is wrong:

  int n;
  cin>>n;
  string s[n+10];

Use vector instead:

  int n;
  cin>>n;
  std::vector<string> s(n);
»
13 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Another bug:
1) Use "return a < b;" in cmp function 2) And use

if (cmp(s[i+2], PLUS(s[i], s[i+1])))

Instead

if(!cmp(PLUS(s[i],s[i+1]),s[i+2]))
  • »
    »
    13 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Accepted! big thanks! :)

    But I don't understand what is difference of them !

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

      There is no problem in

      cmp(PLUS(a, b), c)
      

      here's everything ok. :)

      The potential problem is in

      sort(s.begin(), s.end(), cmp)
      

      because sort and other functions from <algorithm> header expect strict predicate ( less , not lessOrEqual). In some compilers, using lessOrEqual can bring to runtime-assert-check. For example:

      bool BadPredicate(int a, int b)
      {
          return a <= b;
      }
      ...
      int x[] = {1, 2, 1, 2};
      std::sort(x, x + sizeof(x)/sizeof(x[0]), &BadPredicate);
      

      Gives debug-assert in MSVS 2010:

      if (!_Pred(_Left, _Right))
          return (false);
      else if (_Pred(_Right, _Left))
          _DEBUG_ERROR2("invalid operator<", _File, _Line);
      

      But it's not critical for problem acceptance on contest (because problem runs not in debug config), it's right way to make reliable software. :)